How to convert JavaScript array of object to array of string

Posted on January 30, 2022

Using the JavaScript map method easily convert JavaScript array of object to array of string

var itemList = [
    { id: 1000, name: 'Name 1' },
    { id: 1001, name: 'Name 2' },
    { id: 1002, name: 'Name 3' }
];

var idList = itemList.map(function (item) {
    return item['id'];
});

var nameList = itemList.map(function (item) {
    return item['name'];
});

console.log(idList);
console.log(nameList);

//output: [1000, 1001, 1002]
//output: ['Name 1', 'Name 2', 'Name 3']
to join this conversation on Samprix.Already have an account? Sign in to comment
SPONSORS