将分开处理的对象合并成一个包含多个对象的数组。

huangapple go评论94阅读模式
英文:

How do I make separated processed objects into single array with multiple object

问题

将分开的对象重新转换成初始格式的方法是:

  1. let originalFormat = [];
  2. for (let i in csvContent) {
  3. let obj = {
  4. "a": csvContent[i].a,
  5. "b": csvContent[i].b
  6. };
  7. originalFormat.push(obj);
  8. }
  9. console.log(originalFormat);

这将把分开的对象重新组合成初始的格式:[{},{},...]

英文:

I have an array of objects:

  1. csvContent = [{
  2. "a": 123,
  3. "b": "ccc"
  4. },
  5. {
  6. "a": "bbb",
  7. "b": "aaa"
  8. }];

I process first "a" key value to a string use

  1. for (let i in csvContent){
  2. dataString.a = String(csvContent[i].a);
  3. console.log(dataString.a);
  4. }

But the result from the loop is:

result on first loop is

  1. {
  2. "a": "123",
  3. "b": "ccc"
  4. }

result on second loop is

  1. {
  2. "a": "bbb",
  3. "b": "aaa"
  4. }

How to make those separated object back to the beginning format:

> [{},{}]

答案1

得分: 0

你可以尝试使用Array.prototype.map()方法。

> map()方法会在调用数组的每个元素上调用一个提供的函数,并创建一个新数组,其中包含调用结果。

演示:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->

  1. const csvContent = [
  2. { "a": 123, "b": "ccc" },
  3. { "a": "bbb", "b": "aaa" }
  4. ];
  5. const res = csvContent.map(({ a, b }) => ({ a: a.toString(), b: b }));
  6. console.log(res);

<!-- 结束代码片段 -->

英文:

You can try using Array.prototype.map()

>The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const csvContent = [
  2. { &quot;a&quot;: 123, &quot;b&quot;: &quot;ccc&quot; },
  3. { &quot;a&quot;: &quot;bbb&quot;, &quot;b&quot;: &quot;aaa&quot; }
  4. ];
  5. const res = csvContent.map(({ a, b }) =&gt; ({ a: a.toString(), b: b }));
  6. console.log(res);

<!-- end snippet -->

答案2

得分: 0

您不必创建单独的数组,然后将其转换为字符串,然后再次推送。只需在值为字符串时执行以下操作:

  1. csvContent = [{
  2. "a": 123,
  3. "b": "ccc"
  4. },
  5. {
  6. "a": "bbb",
  7. "b": "aaa"
  8. }]
  9. for(let i in csvContent){
  10. console.log(typeof(csvContent[i].a))
  11. if(typeof(csvContent[i].a) != 'string'){
  12. csvContent[i].a = String(csvContent[i].a)
  13. }
  14. }
  15. console.log(csvContent)

这是您提供的代码的翻译部分。

英文:

You dont have to create separate array and convert to string and then again push. Just do like this when value is string

  1. csvContent = [{
  2. &quot;a&quot;: 123,
  3. &quot;b&quot;: &quot;ccc&quot;
  4. },
  5. {
  6. &quot;a&quot;: &quot;bbb&quot;,
  7. &quot;b&quot;: &quot;aaa&quot;
  8. }]
  9. for(let i in csvContent){
  10. console.log(typeof(csvContent[i].a))
  11. if(typeof(csvContent[i].a) != &#39;string&#39;){
  12. csvContent[i].a = String( csvContent[i].a)
  13. }
  14. }
  15. console.log(csvContent)

huangapple
  • 本文由 发表于 2023年5月10日 17:37:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定