I have a javascript object array:
array = [ {x:'x1', y:'y1'}, {x:'x2', y:'y2'}, ... {x:'xn', y:'yn'} ]
I want to create a new array of just the x
values:
[ 'x1', 'x2', ..., 'xn' ]
I could do this easily in a for
loop...:
var newarray = [];
for (var i = 0; i < array.length; i++){
newarray.push(array[i].x);
}
...but I'm wondering if there's a nice one liner way to do this using jquery or even regular javascript?
You can do this with map:
var newarray = jQuery.map(array, function (item) { return item.x; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With