Reputation: 3327
from this array:
var arr = [
{
element: 'anything',
order: 1
},
{
element: 'something',
order: 2
}
]
I want to generate this:
arr = ['anything', 'something'];
How to do this?
Upvotes: 1
Views: 38
Reputation: 442
You can use vanilla js: as shown by @James L.:
arr.map(x => x.element);
or use jQuery Map function:
$.map(arr, function(val, i){
return val.element;
})
See Docs: http://api.jquery.com/jQuery.map/
Upvotes: 1
Reputation: 294
You can use something like
var newArr = [];
$.each(arr, function(index, value) {
newArr.push(value);
});
Upvotes: 1
Reputation: 14515
Use the map function
var arr2 = arr.map(x => x.element);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Upvotes: 4