我有一个对象数组,如下所示:
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"]
有人建议我在做什么错吗?
感谢您的答复!
答案 0 :(得分:2)
为什么不只是简单的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)