This question already has answers here:
From an array of objects, extract value of a property as array
(14个回答)
7个月前关闭。
我有一个对象数组,如下所示:
见上文我尝试过的。但是,我希望获得以下预期输出:
有什么建议我做错了吗?
感谢您的答复!
(14个回答)
7个月前关闭。
我有一个对象数组,如下所示:
let data = [{
name: "Product1",
category: "category 1"
}, {
name: "Product2",
category: "category 2"
}, {
name: "Product3",
category: "category 3"
}, {
name: "Product4",
category: "category 4"
}]
var flattened = [].concat.apply([], data);
console.log(flattened)
// expected output:
// ["Product1", "Product2", "Product3", "Product4"]
见上文我尝试过的。但是,我希望获得以下预期输出:
["Product1", "Product2", "Product3", "Product4"]
有什么建议我做错了吗?
感谢您的答复!
最佳答案
为什么不只是简单的map
let data = [{name: "Product1",category: "category 1"}, {name: "Product2",category: "category 2"}, {name: "Product3",category: "category 3"}, {name: "Product4",category: "category 4"}]
var flattened = data.map(({ name }) => name)
console.log(flattened)
关于javascript - 展平对象数组(键/值对),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57786340/
10-12 01:14