我现在有一个数组对象。该函数应返回所有对象值的数组的数组。哪里错了?
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));
最佳答案
您可以返回对象的值。
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; }
关于javascript - 如何将数组对象转换为数组数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55782295/