Pick data from array of objects and return new object

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pick data from array of objects and return new object



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".


"last"


"first"


"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 at 14:43






Possible duplicate of How do I check if an array includes an object in JavaScript?
– JohnSam
Aug 10 at 15:07




3 Answers
3



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


map


delete


map


destructuring




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:


destructuring


short object literal notation


Object.values(data).map((first, last) => (first, last))



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)





Thanks, how would that look in ES6?
– JohnSam
Aug 10 at 14:48





is the same for es6, map is a array prototype method developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you can use arrow function !
– tierrarara
Aug 14 at 16:19



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);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS