Find best way to find euclidean distance for list of objects in NodeJS

Viewed 178

I have a list of objects as below and need to find euclidean distance of test_obj against all items in the list.

object_list = [ [1,2,3],
                [10,20,30],
                [15,25,35],
                [20,30,40] ]

test_obj = [15,25,35]

I found numpy.linalg.norm() to find this in python. Is there any similar method or utility to do same in NodeJS?

1 Answers

There is a module for Node.js that will accomplish this: euclidean-distance.

We can then use array.map() to get the distance from test_obj to each item in object_list (and vice-versa!), like so:

const distance = require('euclidean-distance')

object_list = [ [1,2,3],
                [10,20,30],
                [15,25,35],
                [20,30,40] ]

test_obj = [15,25,35]

let distances = object_list.map(obj => distance(test_obj, obj));
console.log("Distances:", distances);
Related