首页 > 解决方案 > 加入嵌套数组并输出逗号分隔列表

问题描述

我想打印一个以逗号分隔的数组中的项目列表。

例子:

[
{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输出?

标签: javascriptvue.jsvuejs2

解决方案


你会想要map在你的数组上抓取text道具,然后应用所需的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);


推荐阅读