我有一个对象数组。在每个对象中也是另一个数组。
结构:
function getData() {
return [
{
_id: 1,
tags: ["tomato", "butter", "milk"],
title: "How to make soup?",
category: "Food"
},
{
_id: 2,
tags: ["pepper", "salt", "basil"],
title: "How to use herbs?",
category: "Herbs"
},
];
}
我试图从数组中获取:标签和类别。输出应该是:
{
tags: ["tomato", "butter", "milk"],
category: "Food"
},
{
tags: ["pepper", "salt", "basil"],
category: "Herbs"
},
我尝试了一些概念,但结果并不如我所愿:(
例如:
function getTagsandCategory() {
const tags = getData()
.map(post => {post.tags, post.category});
console.log(tags);
}
getTagsandCategory();
// Output: [undefined, undefined]
或更好(但不幸的是不理想)
function getTagsandCategory() {
const tags = getData()
.map(post => [post.tags, post.category]);
console.log(tags);
}
getTagsandCategory();
// Output: [["tomato", "butter", "milk"], "Food"]
您有什么想法吗,如何实现?谢谢!