Gregori Roberts
Gregori Roberts

Reputation: 309

Search for an object in an array

The function must take the make of the car as the second argument. If the mark is not found, the message “car sold” should be displayed. If the brand is found, it should show the object of the car you were looking for. What is the mistake?

const arr = [{
    "mark": "BMW",
    "price": "55 000",
    "color": "red",
    "constructor": "Billy%Zekun",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "mark": "Mercedes-benz",
    "price": "63 000",
    "color": "blue",
    "constructor": "Jon%Adams",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "mark": "TOYOTA",
    "price": "48 000",
    "color": "blue",
    "constructor": "Jon Hubert",
    "door": "3",
    "max_people": "7",
    "country": "Japan",
    "certificate": "yes"
  },
  {
    "mark": "Volkswagen",
    "price": "36 000",
    "color": "red",
    "constructor": "Pier Sun",
    "country": "Germany",
    "certificate": "no"
  },
];

function car(arr, mark) {
  let carss = arr.filter(function(item) {
    return item.mark === mark;
  })
  return carss !== mark ? carss[0] : "car sold";
};

console.log(car(arr, "Mercedes-benz", ));

Upvotes: 0

Views: 49

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074138

There are two issues:

  1. You can't compare arrays using !== (or !=, or ==, or ===). Even when they have the same contents, two distinct arrays are never equal to each other.

  2. filter isn't the right choice for searching an array for a match. In this case, since you want to return the first car found, you'd use find (with the same predicate function you have now).

Using find:

function car(arr,mark ){
  let car = arr.find(function(item){
     return item.mark === mark ;
  })
  return car || "car sold";
}

or you might use an arrow function

function car(arr,mark ){
  let car = arr.find(item => item.mark === mark);
  return car || "car sold";
}

Live Example:

const arr = [
   {  
     "mark":"BMW",
     "price":"55 000",
     "color":"red",
     "constructor":"Billy%Zekun",
     "country":"Germany",
     "certificate":"yes"
  },
  {  
    "mark":"Mercedes-benz",
    "price":"63 000", 
    "color":"blue",
    "constructor":"Jon%Adams",
    "country":"Germany",
    "certificate":"yes"
  },
  {  
    "mark":"TOYOTA", 
    "price":"48 000", 
    "color":"blue",
    "constructor":"Jon Hubert",
    "door":"3"  ,
    "max_people":"7",
    "country":"Japan",
    "certificate":"yes"
  },
  {  
    "mark":"Volkswagen",
    "price":"36 000", 
    "color":"red",
    "constructor":"Pier Sun",
    "country":"Germany",
    "certificate":"no"
  },
 ];

function car(arr,mark ){
  let car = arr.find(item => item.mark === mark);
  return car || "car sold";
}

console.log(car(arr , "Mercedes-benz"));

(You also have an extra , in your console.log call which I removed in the example. It works because trailing commas are allowed in argument lists in modern JavaScript, but would have failed just a few years ago.)

Upvotes: 1

Related Questions