Home > Net >  Need to separate particular String from array in javascript
Need to separate particular String from array in javascript

Time:05-18

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]

CodePudding user response:

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)

CodePudding user response:

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);

CodePudding user response:

Use Array.map():

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

CodePudding user response:

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

var output = [];

data.map(data => {

data.service

});

  • Related