'Output showing Undefined using find array helper in javaScript

So, here is the code which is using find helper in javaScript and supposed to return a value but instead of that it is showing undefined.

function Car(model) {
this.model = model;
}
var cars = {
 new Car('Buick'),
 new Car('Camaro'),
 new Car('Focus')
};
cars.find(function(car){
 return car.model === 'Focus';
});

I am running this code in VB and the output supposed to be Focus but it is showing undefined.



Solution 1:[1]

Your cars variable needs to be an array not an object as you don't have keys, just a list. Here is a working example:

function Car(model) {
  this.model = model;
}
var cars = [
  new Car('Buick'),
  new Car('Camaro'),
  new Car('Focus')
];
console.log(cars.find((car) => car.model === 'Focus'));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zach Jensz