[英]Join nested array and output comma-separated list
我想打印一個逗號分隔的數組中的項目列表。
例子:
[
{value: 1, text: 'one},
{value: 2, text: 'two},
{value: 3, text: 'three},
{value: 4, text: 'four},
]
我想用 Array.join ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/join ) 解決這個問題 - 但這不適用於包含更多信息的數組,因為輸出是[object Object]
。
我如何“選擇”該值並加入該值,以便獲得one, two, three, four
作為輸出?
您需要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.