从对象数组中获取特定数据并转换为对象

Get specific data from an array of object and convert to object

我有一组对象。在每个对象中也是另一个数组。

结构:

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);

// in single line if you want
console.log(getData().map(({tags,category})=> {return {tags,category}}));

-------------------------------------------------------------------
// if you need more re factoring
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"
    },
  ];
}

function splitData({tags,category} = {}) {
  return {
    tags,
    category
  };
}

var arr = [];

getData().forEach(val => arr.push(splitData(val)))
console.log(arr);

缺少钥匙

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"}];
}

function getTagsandCategory() {
  const tags = getData()
    .map(post => ({
      tags: post.tags,
      category: post.category
    }));

  console.log(tags);
}

getTagsandCategory();

Destructuring assignment接近

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"}];
}

function getTagsandCategory() {
  const tags = getData().map(({tags, category}) => ({ tags, category }));
  console.log(tags);
}

getTagsandCategory();

只需使用映射和解构即可。

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"
    },
  ];
}

console.log(getData().map(({tags, category}) => ({tags, category})));