Hi I have json return object like this
color_selected = [ { id: 4}, { id: 3} ];
how do I convert it to
color_selected = [4,3]
thank you for your any help and suggestions
You could iterate through it like this:
var newArray = []; for(var i = 0; i < color_selected.length; i++) { newArray.push(color_selected[i].id); }
you can use javascript map
function for that
var newArray = color_selected.map(o=> o.id)
var color_selected = [ { id: 4}, { id: 3} ]; var newArray = color_selected.map(o=> o.id) console.log(newArray)
map
is functional programming method. it's better if you can follow array methodCommentedMay 12, 2017 at 17:32color_selected = [ { id: 4}, { id: 3} ];
You can use lodash
// in 3.10.1
_.pluck(color_selected, 'id'); // → [4, 3] _.map(color_selected, 'id'); // → [4, 3]
// in 4.0.0
_.map(color_selected, 'id'); // → [4, 3]
Use Array.map() method with ES6
Arrow operator.
var color_selected = [ { id: 4}, { id: 3} ]; color_selected = color_selected.map(item => {return item.id }); console.log(color_selected);