개체 배열이 있습니다. 각 개체에는 또 다른 배열이 있습니다.
구조:
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"]
그것을 달성하는 방법에 대한 아이디어가 있습니까? 감사합니다!
당신은 사용해야합니다 Array.prototype.map()
const data = [
{
_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"
},
];
const output = data.map(obj => ({ tags: obj.tags, category: obj.category }));
console.log(output);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다