JavaScript Get specific data from an array of object and convert to object specific data to,extract specific data from text file python,

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!

Answer:1

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);
Answer: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();
Answer:3

// 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);
Answer:4

I'm trying to implement a worker module into my app. The app itself is using require.js to load all the pre-requesites. And this is where all the issues with loading worker-module begin. index.html ...

I'm trying to implement a worker module into my app. The app itself is using require.js to load all the pre-requesites. And this is where all the issues with loading worker-module begin. index.html ...

I have a table and it looks something like this: | Company | Contact | Country | |---|---|---| | Alfreds Futterkiste | Maria Anders | Germany | | Centro Moctezuma | ...

I have a table and it looks something like this: | Company | Contact | Country | |---|---|---| | Alfreds Futterkiste | Maria Anders | Germany | | Centro Moctezuma | ...

So I am new to React and trying to write a Todo list. As you will see in the following snippets I define an array of sample todos in the App.js. I call the TodoList component in the App.js and hand ...

So I am new to React and trying to write a Todo list. As you will see in the following snippets I define an array of sample todos in the App.js. I call the TodoList component in the App.js and hand ...

  1. pass value from child to parent react
  2. pass value from child to parent angular
  3. pass value from controller to view
  4. pass value from html to javascript
  5. pass value from aura to lwc
  6. pass value from javascript to php
  7. pass value from component to component angular
  8. pass value from parent to child lwc
  9. pass value from javascript to c#
  10. pass value from flow to lightning component
  11. pass value from html to ts angular
  12. pass value from child to parent vue
  13. pass value from parent to child angular 8
  14. pass value from process builder to flow
  15. pass value from button to javascript
  16. pass value from javascript to visualforce page
  17. pass value from js to html
  18. pass value from visualforce component to controller
  19. pass value from one javascript to another
  20. pass value from input to javascript function

I'm trying to "insert" an alerter if the username of a basic form is not filled and I have been helplessly trying to understand why the anonymous function within a submit function doesn't get called. ...

I'm trying to "insert" an alerter if the username of a basic form is not filled and I have been helplessly trying to understand why the anonymous function within a submit function doesn't get called. ...

  1. calling anonymous function matlab
  2. calling anonymous function javascript
  3. calling anonymous function scala
  4. calling anonymous function jquery
  5. self calling anonymous function javascript
  6. javascript calling anonymous function recursively
  7. java calling anonymous function
  8. anonymous function calling itself