需要在 javascript 中将特定字符串与数组分开
Need to separate particular String from array in javascript
我有这样的输出
输出:
[{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]
我需要这样转换
输出:[xdc,cddc,cdcd,cddc]
您可以使用Array.prototype.map() combined with Destructuring assignment
代码:
const output = [{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]
const result = output.map(({ service }) => service)
console.log(result)
在这种情况下,您可以使用简单的 forEach()
循环:
var data = [{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]
var output = [];
data.forEach(element => {
output.push(element.service);
});
console.log(output);
使用Array.map():
const output=[{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]
const result = output.map(el => el.service)
console.log(result)
var datas = [{服务:'xdc'},{服务:'cddc'},{服务:'cdcd'},{服务:'cddc'}]
data.map(数据 => data.service);
我有这样的输出
输出:
[{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]
我需要这样转换
输出:[xdc,cddc,cdcd,cddc]
您可以使用Array.prototype.map() combined with Destructuring assignment
代码:
const output = [{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]
const result = output.map(({ service }) => service)
console.log(result)
在这种情况下,您可以使用简单的 forEach()
循环:
var data = [{service: 'xdc'},{service: 'cddc'}, {service:
'cdcd'},{service: 'cddc'}]
var output = [];
data.forEach(element => {
output.push(element.service);
});
console.log(output);
使用Array.map():
const output=[{service: 'xdc'},{service: 'cddc'}, {service: 'cdcd'},{service: 'cddc'}]
const result = output.map(el => el.service)
console.log(result)
var datas = [{服务:'xdc'},{服务:'cddc'},{服务:'cdcd'},{服务:'cddc'}]
data.map(数据 => data.service);