0

I have an array object now. The function should return an array of arrays of all object values. Where is the mistake?

const car = [
  {  
    "name":"BMW",
    "price":"55 000",
    "country":"Germany",
    "security":"Hight"
  },
  {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
  },
  {  
    "name":"Mercedes-benz",
    "price":"63 000", 
    "country":"Germany",
    "security":"Hight"
  }
 ];

function cars(car){
  return car.map(function(key) {
    return [[key]];
  });
}
console.log(cars(car));

3

3 Answers 3

3

You could return the values of the object.

function cars(car){
    return car.map(Object.values);
}

const car = [{ name: "BMW", price: "55 000", country: "Germany", security: "Hight" }, { name: "Mitsubishi", price: "93 000", constructor: "Bar John", door: "3", country: "Japan" }, { name: "Mercedes-benz", price: "63 000", country: "Germany", security: "Hight" }];

console.log(cars(car));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

You are wrapping the individual array object in another array so it becomes [[{object}]], simply map to a new array of Object.values of the inner objects.

const car = [
  {  
    "name":"BMW",
    "price":"55 000",
    "country":"Germany",
    "security":"Hight"
  },
  {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
  },
  {  
    "name":"Mercedes-benz",
    "price":"63 000", 
    "country":"Germany",
    "security":"Hight"
  }
 ];

function cars(car){
  return Array.from(car, Object.values)
}
console.log(cars(car));

Comments

0

Change [[key]] to [key]

const car = [
  {  
    "name":"BMW",
    "price":"55 000",
    "country":"Germany",
    "security":"Hight"
  },
  {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
  },
  {  
    "name":"Mercedes-benz",
    "price":"63 000", 
    "country":"Germany",
    "security":"Hight"
  }
 ];

function cars(car){
  return car.map(function(key) {
    return [key];
  });
}
console.log(cars(car));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.