1

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?

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Or even more concise: arr.map(x => x.element) ;)
And, perhaps worth noting, no jQuery needed-- this is vanilla JS.
Worth noting that arrow functions do not work in Internet Explorer 11 (for those stuck with legacy users).
1

You can use something like

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

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.