How to convert convert object to array

user2062455

I am trying to convert an object to an array with pure javascript.

I want to convert this:

[{"itemCode":"Mob-mtr"},{"itemCode":"640-chr"}]

to this:

["Mob-mtr","640-chr","541-mtr"]

i have tried this code:

var arr = Object.keys(obj).map(function (key) {return obj[key]});

and a bunch of other variations with no success.

Any idea how i can convert this object to an array?

Nina Scholz

You can use the property directly for the return value.

var array = [{ "itemCode": "Mob-mtr" }, { "itemCode": "640-chr" }],
    result = array.map(function (a) { return a.itemCode; });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related