Home > database >  Output showing Undefined using find array helper in javaScript
Output showing Undefined using find array helper in javaScript

Time:03-18

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.

CodePudding user response:

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'));

  • Related