简体   繁体   English

如何将相同的密钥 JavaScript Object 动态组合到一个数组中

[英]How to Combine Same key JavaScript Object to one Array Dynamically

How to Combine Same key JavaScript Object to one Array Dynamically in JavaScript/Nodejs and Produce given Input to expected output,如何在 JavaScript/Nodejs 中动态地将相同的键 JavaScript Object 组合到一个数组中,并生成给定的输入到预期的 output,

Please do help me on this.请帮我解决这个问题。 that would be really helpful.那真的很有帮助。

Example -例子 -

[
  {
    1: { date: 1638334800, value: 0 },
    2: { date: 1638334800, value: 19.71 }
    nth: { date: 1638334800, value: 19.71 }
  },
  {
    1: { date: 1638334900, value: 0 },
    2: { date: 1638334800, value: 23.77 }
    nth: { date: 1638334800, value: 29.71 }
  }
]

Expected Output -预计 Output -

    1: {
      timeSeriesData: [
        {
          date: 1638334800,
          value: 0,
        },
        {
          date: 1638334900,
          value: 0,
        } 
      ], units: '%'
    },
    2: {
      timeSeriesData: [
        {
          date: 1638334800,
          value: 19.71,
        },
        {
          date: 1638334800,
          value: 19.71,
        }
      ], units: '%'
    },
   nth: {
      timeSeriesData: [
        {
          date: 1638334800,
          value: 19.71,
        },
        {
          date: 1638334800,
          value: 29.71,
        }
      ], units: '%'
    }

You can just iterate over your input data's keys.您可以迭代输入数据的键。 Then if the key already exists in your results you append the new value, otherwise create an array with the first value within your results.然后,如果您的结果中已经存在该键,则 append 新值,否则使用结果中的第一个值创建一个数组。

Here's a working demo:这是一个工作演示:

 const sample = [ { 1: { date: 1638334800, value: 0 }, 2: { date: 1638334800, value: 19.71 }, nth: { date: 1638334800, value: 19.71 } }, { 1: { date: 1638334800, value: 0 }, 2: { date: 1638334800, value: 23.77 }, nth: { date: 1638334800, value: 19.71 } } ]; const generateOutput = (inputData) => { const results = {}; inputData.forEach((dataSet) => { Object.keys(dataSet).forEach((key) => { if (results[key]) { results[key].push(dataSet[key]); } else { results[key] = [dataSet[key]]; } }); }); return results; } console.log(generateOutput(sample));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中合并具有相同键的数组中的对象? - how to combine objects in an array that have the same key in javascript? 如何将具有相同键的对象数组合到一个对象中? - How can I combine an array of objects with the same keys into one object? 如何动态重命名 javascript 中 object 嵌套数组中的 object 键 - how to rename the object key in nested array of object in javascript dynamically 如何将其与一个javascript对象组合? - How to combine this to one javascript Object? 将两个数组转换为一个对象,Javascript中每个数组的键相同 - Convert two arrays to one object, with same key for each array in Javascript 如何破坏对象中的数字数组并在javascript中合并为一个? - How to destruct array of numbers in an object and combine to one in javascript? 将具有相同日期的数组对象合并为一个 - JavaScript - Combine objects of an array with the same date into one - JavaScript 如何从数组字符串动态创建 JavaScript 中的对象键和值 - how to create Object key & value in JavaScript dynamically from array string 如何将数组与对象和键组合成多维数组 - How to combine array to array multidimensional with object and key 如何将数组转换为 object 并在此 object Javascript 中添加相同的密钥? - How to convert array to object with add same key in this object Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM