Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  150] [ 2]  / answers: 1 / hits: 79319  / 9 Years ago, wed, december 16, 2015, 12:00:00

Is there a simple way, using filter or parse or something else to convert an array like the following :



var someJsonArray = [
{id: 0, name: name, property: value, otherproperties: othervalues},
{id: 1, name: name1, property: value1, otherproperties: othervalues1},
{id: 2, name: name2, property: value2, otherproperties: othervalues2}
];


into a simple array filled with one attribute of the objects contained in the previous array like this :



[0, 1, 2]

More From » arrays

 Answers
22

Use .map() function:



finalArray = someJsonArray.map(function (obj) {
return obj.id;
});


Snippet





var someJsonArray = [
{id: 0, name: name, property: value, therproperties: othervalues},
{id: 1, name: name1, property: value1, otherproperties: othervalues1},
{id: 2, name: name2, property: value2, otherproperties: othervalues2}
];
var finalArray = someJsonArray.map(function (obj) {
return obj.id;
});
console.log(finalArray);





The above snippet is changed to make it work.


[#64054] Sunday, December 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;