user4157552
user4157552

Reputation:

Convert array in the list of values in JavaScript

I have array with such values:

 [{"meta_value":"Germany"},{"meta_value":"United States"}]

I need someway to convert it for list of values for autocomplete: [Germany, United States].

How to do it in the proper way?

Upvotes: 0

Views: 42

Answers (2)

Amir Popovich
Amir Popovich

Reputation: 29836

Use Array.prototype.map() to map one array to another array based on a mapping function:

var metaArr = [{"meta_value":"Germany"},{"meta_value":"United States"}];
var arrayOfStates = metaArr.map(function(item){return item.meta_value});

Simply use arrayOfStates as your source for the autocompletion.

Upvotes: 1

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use map

var res = [{"meta_value":"Germany"},{"meta_value":"United States"}].map(function (el) {
   return el.meta_value;
})

Upvotes: 2

Related Questions