0

I'm new to Node.js and I want to access a particular set of information from an object that looks like this:

userData: [
  {
    value: false,
    id: 1,
    title: 'Hello World',
    date: '17 February 2018 - 06:27:51 PM',
    status: 'Processing'
  },
  {
    value: false,
    id: 2,
    title: 'Hello People',
    date: '17 February 2018 - 06:27:48 PM',
    status: 'Active'
  },
  {
    value: false,
    id: 3,
    title: 'Hello Canary',
    date: '17 February 2018 - 06:27:44 PM',
    status: 'Expired'
  }
]

Now I want to return an array that consist of only "title"

Example: ['Hello World', 'Hello people', 'Hello Canary']

I have tried Object.values(obj) and Object.keys(yourObject).map(key => yourObject[key]) without any luck. Can anyone please help me?

Update: I'm using ES6 and would like to optimize my code for performance

Dev Aggarwal
  • 763
  • 1
  • 6
  • 19

3 Answers3

4

let titles = userData.map(x => x.title);

When using Array.prototype.map with an array. It returns an array made up of the return value of the function you pass it, called on each element of the array. So the above example, when used on your array of objects containing the .title property, will return a new array containing the titles of each object in the original array.

Helam
  • 1,385
  • 13
  • 17
  • Your answer is partially right and it will require reassigning the processed output that is a reason I gave the downvote to you. – Dipak Mar 01 '18 at 17:15
  • I tired using `return userData.map(x => x.title)` in a function. Here's the error for the same: `TypeError: Cannot read property 'header' of undefined` – Dev Aggarwal Mar 01 '18 at 17:36
2

Here's a working snippet

var userData = [{
    value: false,
    id: 1,
    title: 'Hello World',
    date: '17 February 2018 - 06:27:51 PM',
    status: 'Processing'
  },
  {
    value: false,
    id: 2,
    title: 'Hello People',
    date: '17 February 2018 - 06:27:48 PM',
    status: 'Active'
  },
  {
    value: false,
    id: 3,
    title: 'Hello Canary',
    date: '17 February 2018 - 06:27:44 PM',
    status: 'Expired'
  }
]

// Solution to your problem
let titles = userData.map(function(obj) {
  return obj.title;
});

console.log(titles);

Explanation:

  1. Map will execute on each object of the User Data array
  2. Map method takes in a function which returns values that we want in our final array, which is, in this case 'title'
  3. So after Map is done iterating through the whole list, titles of each object in the User Data array, will be collected in titles variable
Abhijit Kar ツ
  • 1,732
  • 1
  • 11
  • 24
0
Try this:
var userData =  [
        {
          value: false,
          id: 1,
          title: 'Hello World',
          date: '17 February 2018 - 06:27:51 PM',
          status: 'Processing'
        },
        {
          value: false,
          id: 2,
          title: 'Hello People',
          date: '17 February 2018 - 06:27:48 PM',
          status: 'Active'
        },
        {
          value: false,
          id: 3,
          title: 'Hello Canary',
          date: '17 February 2018 - 06:27:44 PM',
          status: 'Expired'
        }
      ];
      var newArray=[];
userData.forEach(function(entry) {
    newArray.push(entry.title);
});
alert(newArray);
Bimlendu Kumar
  • 226
  • 2
  • 17