javascript - 加入嵌套数组并输出逗号分隔列表
问题描述
我想打印一个以逗号分隔的数组中的项目列表。
例子:
[
{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
道具,然后应用所需的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);
推荐阅读
- javascript - 记录正在更新,但我仍然在控制台中收到此错误
- jquery-ui - jquery_ui 动态子菜单的菜单ui参数
- python - 从熊猫数据框中同一字段的所有其他行中减去一行字段中的值
- javascript - React - createPortal 或 appendChild 的仅附加行为?
- reactjs - 摇树不适用于使用命名导入的反应组件库
- html - How do I put a border around each table box?
- mysql - 在自动列表中打印帖子(pug、mysqlm、node.js、express)
- windows - nt!KiSystemCall64 中的断点设置不起作用
- mysql - 如何通过条件指定的表之一将表与树表连接起来
- r - 如何将 120/80 变成两列(120 和 80)?