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

4
  • 2
    Possible duplicate of From an array of objects, extract value of a property as array Commented Mar 1, 2018 at 17:10
  • @MikeMcCaughan No luck with var result = objArray.map(a => a.foo); Commented Mar 1, 2018 at 17:31
  • That's exactly what your accepted answer does. Commented Mar 1, 2018 at 17:51
  • Yeah. I do realize that. It wasn't working before though. Commented Mar 2, 2018 at 6:34

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is partially right and it will require reassigning the processed output that is a reason I gave the downvote to you.
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
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

3 Comments

Thank you for the explanation
I got it working. I was using Camel case in property. Silly me :D
Good to hear that. :D
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.