1

I am getting an array of data containing many parameters. For example, the data array is in the below format:

var data  = [
             {
               "name":"pradeep", 
               "age": "32", 
               "location": "Bangalore"
             }, 
             {
               "name":"praveen", 
               "age": "30", 
               "location": "Mangalore"
             }, 
             {
               "name":"Ajay", 
               "age": "32", 
               "location": "Hubli"
             }
           ]

I want the above array to be reduced to the below format:

     [
             {
               "name":"pradeep"
             }, 
             {
               "name":"praveen"
             }, 
             {
               "name":"Ajay"
             }
           ]

How to get this done using Underscore. I tried using the _.pluck but it fetches only the data and not the key.

5
  • Check out the _.pick or _.omit functions. They are functions that filter objects. Commented Jan 11, 2016 at 17:15
  • @DenisFrezzato I wrote an answer recommending _.pick, then I realised the OP wants an array of objects and pick only works on single objects. Commented Jan 11, 2016 at 17:17
  • It can be achieved using _.map to loop through the array and _.pick to filter the object. Commented Jan 11, 2016 at 17:20
  • @DenisFrezzato indeed, but it's hardly efficient. _.pick() is best suited for when there are multiple keys to be copied, and/or when the keyname is variable. Commented Jan 11, 2016 at 17:22
  • I agree with you, but @Pradeep has specified Underscore... Commented Jan 11, 2016 at 17:23

6 Answers 6

1

You could use pure JavaScript to do it, one way is:

data.map(function(obj) { 
    return {'name': obj.name};
})

If you'd like to parametrize key that should be kept, you may write function like that:

function filterByKey(arr, key) {
    return arr.map(function(obj) {
        var newObj = {};
        newObj[key] = obj[key];
        return newObj;
    });
}

and use it like that:

var newData = filterByKey(data, 'name');

Cheers

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

Comments

0

In native JavaScript, you can do it like:

var data = [{
  "name": "pradeep",
  "age": "32",
  "location": "Bangalore"
}, {
  "name": "praveen",
  "age": "30",
  "location": "Mangalore"
}, {
  "name": "Ajay",
  "age": "32",
  "location": "Hubli"
}];

var newData = data.map(function(d) {
  return { name: d.name }
});

console.log(newData)

Comments

0

Underscore is not really required, you can do this..

var newData = data.map(function (x) {
    return { name: x.name };
});

but if you really want to use underscore this is the way:

var newData = _.map(data, function (x) {
    return { name: x.name };
});

Comments

0

For a strictly underscore JS method you can use _.map combined with _.pick:

var newData = _.map(data, function(d) { return _.pick(d, 'name' })

However using _.pick will not be as efficient as just simply returning { name: d.name } in the _.map callback.

Comments

0

You need to use the map function from Undescore. It's a very popular utility from the functional programming and you can also use it natively in the browser.

With Underscore.js the code looks like this:

_.map(data, function(element) {
  return {name: element['name']};
})

Comments

0

You can do it using native JavaScript

var result = [];
data.forEach(function(d){
 result.push({'name': d.name}) // d is an iterator
})

You could as use map function to return the key value pair

var result = data.map(function(d){
 return {'name': d.name}
})

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.