'Need to separate particular String from array in javascript

I have a output like this

Output :

[{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]

I need to convert like this

output : [xdc,cddc,cdcd,cddc]



Solution 1:[1]

You can use Array.prototype.map() combined with Destructuring assignment

Code:

const output = [{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]

const result = output.map(({ service }) => service)

console.log(result)

Solution 2:[2]

Use Array.map():

const output=[{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]
const result = output.map(el => el.service)
console.log(result)

Solution 3:[3]

You can use simple forEach() loop in this case:

var data = [{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]

var output = [];

data.forEach(element => {
output.push(element.service);
});

console.log(output);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 VMT
Solution 3 Sumit Sharma