[英]map object.keys into string from array of objects
I have an array, this for example: 我有一个数组,例如:
interface: [
{ _id: '1', name: 'Foo' },
{ _id: '2', name: 'bar },
{ _id: '3', name: 'boo}
]
i'd now like to map this to a single string -> 我现在想将此映射到单个字符串->
this.dataSource.data = data.data.map(item => ({
interface: item.interfaces.name,
}));
so that the result inside the dataSource.data.interface
looks like 'Foo, bar, boo'
因此
dataSource.data.interface
内的结果看起来像'Foo, bar, boo'
You could map the name
property and join the array. 您可以映射
name
属性并加入数组。
var data = { interface: [{ _id: '1', name: 'Foo' }, { _id: '2', name: 'bar' }, { _id: '3', name: 'boo' }]}; data.interface = data.interface.map(({ name }) => name).join(', '); console.log(data);
Use Array.prototype.reduce: 使用Array.prototype.reduce:
const concatString = data.interface.reduce((acc, value, index, arr) => {
const str = index !== arr.length - 1 ? `${value.name}, ` : value.name;
return acc += str;
}, "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.