【问题标题】:Way to save all elements inside an array of object in React在 React 中将所有元素保存在对象数组中的方法
【发布时间】:2019-09-05 07:39:00
【问题描述】:

我想将所有记录值保存在一个带有逗号 (,) 分隔符的变量中。有什么办法吗?

预期内容= 13783107、13819738、13819745 ...等

这是文件的内容

Object {metaData: Array(1), rows: Array(29)}
metaData:Array(1) [Object]
rows:Array(29) [Object, Object, Object, …]
length:29
__proto__:Array(0) [, …]
0:Object {RECORDS: 13783107}
1:Object {RECORDS: 13819738}
2:Object {RECORDS: 13819745}
3:Object {RECORDS: 13819746}
4:Object {RECORDS: 13819747}

谢谢。

【问题讨论】:

  • 预期内容的类型是什么?字符串?

标签: javascript reactjs


【解决方案1】:

说,变量file 包含文件。试试这个:

const commaSeparatedRecords = file.rows.map(r => r.RECORDS).join(', ');
console.log(commaSeparatedRecords);

在每个数字前后添加一个单引号 (') 怎么样?

file.rows.map(r => `'${r.RECORDS}'`).join(', ');
// note backtick 

【讨论】:

  • 谢谢,file.rows.map(r => r.RECORDS).join(', ') 有效。在每个数字前后添加一个单引号 (') 怎么样?
  • 返回反引号'${r.RECORDS}'反引号。更新答案
猜你喜欢
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2019-11-26
  • 2021-12-17
相关资源
最近更新 更多