2

I have an array of objects. In the each object is also another array.

Structure:

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

I am trying to get from the array: tags and category. Output should be:

{
      tags: ["tomato", "butter", "milk"],
      category: "Food"
},
 {
      tags: ["pepper", "salt", "basil"],
      category: "Herbs"
},

I tried a few concepts, but result is not as I wish :(

For example:

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

 console.log(tags);
}

getTagsandCategory(); 
// Output: [undefined, undefined]

or better (but unfortunately not ideal)

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

 console.log(tags);
}

getTagsandCategory();
// Output: [["tomato", "butter", "milk"], "Food"]

Do you have please any idea, how to achieve that? Thank you!

tomasconnor
  • 121
  • 8

4 Answers4

3

You need to use 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);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
2

Missing keys

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 approach

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();
Ele
  • 33,468
  • 7
  • 37
  • 75
1

// 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);
pavan kumar
  • 823
  • 6
  • 15
0

Just use a map and destructuring.

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})));
Bibberty
  • 4,670
  • 2
  • 8
  • 23