简体   繁体   中英

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] .

How can I "pick" the value and join the value so I'm getting one, two, three, four as output?

You'll want to map over your array to grab the text prop out of it and then apply the desired 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(', ')}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM