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)
color_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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.