[英]Join nested array and output comma-separated list
I would like to print a comma-separated list of items in an array.我想打印一个逗号分隔的数组中的项目列表。
Example:例子:
[
{value: 1, text: 'one},
{value: 2, text: 'two},
{value: 3, text: 'three},
{value: 4, text: 'four},
]
I thought about solving this with Array.join ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/join ) - but this is not working with arrays containing more information, as the output is [object Object]
.我想用 Array.join ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/join ) 解决这个问题 - 但这不适用于包含更多信息的数组,因为输出是
[object Object]
。
How can I "pick" the value and join the value so I'm getting one, two, three, four
as output?我如何“选择”该值并加入该值,以便获得
one, two, three, four
作为输出?
You'll want to map
over your array to grab the text
prop out of it and then apply the desired join
.您需要
map
数组以从中获取text
prop,然后应用所需的join
。
const arr = [ {value: 1, text: 'one'}, {value: 2, text: 'two'}, {value: 3, text: 'three'}, {value: 4, text: 'four'} ]; const output = arr.map(el => el.text).join(', '); console.log(output);
找到了解决办法:
{{array.map(x => x.text).join(', ')}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.