user1896653
user1896653

Reputation: 3327

how to remove object property and take specific values from array by jQuery

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

Answers (3)

Mateus Martins
Mateus Martins

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

Jason
Jason

Reputation: 294

You can use something like

var newArr = [];
$.each(arr, function(index, value) {
    newArr.push(value);
});

Upvotes: 1

James L.
James L.

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

Related Questions