Reputation: 309
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));
Upvotes: 0
Views: 84
Reputation: 386634
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; }
Upvotes: 3
Reputation: 16132
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));
Upvotes: 0
Reputation: 16908
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));
Upvotes: 0