-6

So, I have:

"people": [
                {
                    "first": "Linda",
                    "last": "Donson",
                    "salary": "4000USD"
                },
                {
                    "first": "Mark",
                    "last": "Sullivan",
                    "salary": "3500USD"
                },
        ];

Now, I want to convert this into a new array of objects, containing only desired set of data. For example:

"people": [
                    {
                        "first": "Linda",
                        "salary": "4000USD"
                    },
                    {
                        "first": "Mark",
                        "salary": "3500USD"
                    },
            ];

How can I do this? So, it should take out the "last" key and its values and return only "first" and "last".

Thanks

  • Please show what you have tried. Deleting properties in objects is not hard to research and so is looping over arrays of objects and copying objects. Stackoverflow is not a free code writing service. The objective here is for others to help you fix **your code**. Even if you only got part of it sorted out and the rest isn't working as expected that would show some effort which is expected – charlietfl Aug 10 '18 at 14:43
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) –  Aug 10 '18 at 15:07

3 Answers3

1

you can use map function to get a new array with your desired options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var source = [ { first: 'first1', last: 'last1', property1: 'p1-1' }, { first: 'first2', last: 'last2', property1: 'p1-2' }];

var mapped = source.map( function (e) {
  return { first: e.first, property1: e.property1 }
})

console.log('mapped : ', mapped)
tierrarara
  • 61
  • 8
  • Thanks, how would that look in ES6? –  Aug 10 '18 at 14:48
  • is the same for es6, map is a array prototype method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map you can use arrow function ! – tierrarara Aug 14 '18 at 16:19
1

This really is a simple one liner via map and delete or map and ES6 destructuring as shown in the 2 examples bellow:

var data = [{
    "first": "Linda",
    "last": "Donson",
    "salary": "4000USD"
  },
  {
    "first": "Mark",
    "last": "Sullivan",
    "salary": "3500USD"
  }
]

console.log(data.map(x => delete(x.salary) && x))

Also if you are concerned about mutating the object you can simply use ES6 destructuring and the short object literal notation to get this:

Object.values(data).map(({first, last})  => ({first, last}))
Akrion
  • 18,117
  • 1
  • 34
  • 54
0

You can use forEach

var peoples = [{
        "first": "Linda",
        "last": "Donson",
        "salary": "4000USD"
    },
    {
        "first": "Mark",
        "last": "Sullivan",
        "salary": "3500USD"
    },
];


// empty array 
var newPeoples = [];


peoples.forEach((people) => {

    // only push desired object inside empty array
    newPeoples.push({
        "first": people.first,
        "salary": people.salary
    });

});


// desired array of objects
console.log(newPeoples);
sparsh turkane
  • 1,165
  • 12
  • 13