简体   繁体   English

使用Lodash groupby和map变换对象数组

[英]Transform Array of object using Lodash groupby and map

Here is what I want to do, I want to take the following array and turn it into the array after it. 这是我想做的,我想采用以下数组,然后将其转换为数组。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Original Data 原始数据

[{
        "FeatureId": 1,
        "District": "ANE",
        "Temperature": 206,
        "RelativeHumidity": 20,
        "WindSpeed": 5,
        "WindGust": 15
      },
      {
        "FeatureId": 1,
        "District": "ANE",
        "Temperature": 196,
        "RelativeHumidity": 19,
        "WindSpeed": 5,
        "WindGust": 15
      },
      {
        "FeatureId": 2,
        "District": "AMO",
        "Temperature": 203,
        "RelativeHumidity": 54,
        "WindSpeed": 9,
        "WindGust": 18
      },
      {
        "FeatureId": 2,
        "District": "AMO",
        "Temperature": 184,
        "RelativeHumidity": 46,
        "WindSpeed": 12,
        "WindGust": 18
      }]

My Desired Data : 我想要的数据:

[
  {
    "FeatureId": 1,
    "District": "ANE",
    "TemperatureTrend": [ 206, 196 ],
    "RelativeHumidityTrend": [ 20, 19 ],
    "WindSpeedTrend": [ 5, 5 ],
    "WindGustTrend": [ 15, 15 ]
  },
  {
    "FeatureId": 2,
    "District": "AMO",
    "TemperatureTrend": [ 203, 184 ],
    "RelativeHumidityTrend": [ 54, 46 ],
    "WindSpeedTrend": [ 9, 12 ],
    "WindGustTrend": [ 18, 18 ]
  },

]

I have tried combining groupby and map, but I was unable to resolve the issue. 我尝试将groupby和map结合使用,但无法解决问题。 I was also unsure how to log the steps. 我也不确定如何记录步骤。

Apart from other good answers, you can use es6 destructuring as well. 除了其他好的答案之外,您还可以使用es6解构。

 var arr = [{ "FeatureId": 1, "District": "ANE", "Temperature": 206, "RelativeHumidity": 20, "WindSpeed": 5, "WindGust": 15 }, { "FeatureId": 1, "District": "ANE", "Temperature": 196, "RelativeHumidity": 19, "WindSpeed": 5, "WindGust": 15 }, { "FeatureId": 2, "District": "AMO", "Temperature": 203, "RelativeHumidity": 54, "WindSpeed": 9, "WindGust": 18 }, { "FeatureId": 2, "District": "AMO", "Temperature": 184, "RelativeHumidity": 46, "WindSpeed": 12, "WindGust": 18 }] var temp = arr.reduce((o, d) => ( { WindSpeed, WindGust, RelativeHumidity, Temperature, ...rest } = ({ ...d , ...{ RelativeHumidityTrend: (o[d.FeatureId] && o[d.FeatureId].RelativeHumidityTrend || []).concat(d.RelativeHumidity) , WindSpeedTrend: (o[d.FeatureId] && o[d.FeatureId].WindSpeedTrend || []).concat(d.WindSpeed) , WindGustTrend: (o[d.FeatureId] && o[d.FeatureId].WindGustTrend || []).concat(d.WindGust) , TemperatureTrend: (o[d.FeatureId] && o[d.FeatureId].TemperatureTrend || []).concat(d.Temperature) } }) , o[d.FeatureId] = rest , o ) , {}) var result = Object.values(temp) console.log(result) 

Lets use Array.prototype.reduce because it fit the purpose of what you are trying to do. 让我们使用Array.prototype.reduce,因为它符合您要执行的操作的目的。

    const data = [{
        "FeatureId": 1,
        "District": "ANE",
        "Temperature": 206,
        "RelativeHumidity": 20,
        "WindSpeed": 5,
        "WindGust": 15
      },
      {
        "FeatureId": 1,
        "District": "ANE",
        "Temperature": 196,
        "RelativeHumidity": 19,
        "WindSpeed": 5,
        "WindGust": 15
      },
      {
        "FeatureId": 2,
        "District": "AMO",
        "Temperature": 203,
        "RelativeHumidity": 54,
        "WindSpeed": 9,
        "WindGust": 18
      },
      {
        "FeatureId": 2,
        "District": "AMO",
        "Temperature": 184,
        "RelativeHumidity": 46,
        "WindSpeed": 12,
        "WindGust": 18
      }]

// we gonna create from scratch array and populate it
const aggregatedData = data.reduce((memo, element) => {
  const featureId = element.FeatureId

  const elementIndex = memo.findIndex(el => el.FeatureId === featureId)
  if (elementIndex === -1) {
    memo.push({
      FeatureId: featureId,
      District: element.District,
      TemperatureTrend: [element.Temperature],
      RelativeHumidityTrend: [element.RelativeHumidity],
      WindSpeedTrend: [element.WindSpeed],
      WindGustTrend: [element.WindGust]
    })
  } else {
    memo[elementIndex].TemperatureTrend.push(element.Temperature)
    memo[elementIndex].RelativeHumidityTrend.push(element.RelativeHumidity)
    memo[elementIndex].WindSpeedTrend.push(element.WindSpeed)
    memo[elementIndex].WindGustTrend.push(element.WindGust)
  }

  return memo
}, [])

console.log(aggregatedData)

You can check it in codepen 您可以在Codepen中检查它

Christian, welcome to StackOverflow! 克里斯蒂安,欢迎来到StackOverflow!

You have an interesting, multi-step logic problem. 您有一个有趣的,多步骤的逻辑问题。 I am assuming you may have more than just 2 of each FeatureId, and in fact I will assume you could have anywhere from 1...n of FeatureId. 我假设每个FeatureId可能不止2个,实际上,我假设FeatureId的范围是1 ... n。

I would first use a forEach loop to make an array of all your unique FeatureId values. 我将首先使用forEach循环将所有唯一FeatureId值组成一个数组。 Then, now knowing what those values are, I would use JavaScript's filter to create a new array that contains only those objects with the same FeatureId. 然后,现在知道这些值是什么,我将使用JavaScript的过滤器创建一个新数组,该数组仅包含具有相同FeatureId的那些对象。

The last step, combining the objects, will be much easier now that all elements in these "working arrays" have the same FeatureId. 由于这些“工作数组”中的所有元素都具有相同的FeatureId,因此将对象组合在一起的最后一步会容易得多。 If there is only one observation object, you can push the existing object onto a final, finished array. 如果只有一个观察对象,则可以将现有对象推到最终的成品阵列上。 If there is >1 observation object, then you can use your combining logic and push a new concatenated object onto your final, finished array. 如果存在> 1个观察对象,则可以使用合并逻辑并将新的串联对象推入最终的成品阵列中。

Your idea of using lodash map is headed in the right direction, but 1) see if you can do it without Lodash and 2) I think a combination of filter and forEach will work better for you. 您使用lodash贴图的想法朝着正确的方向发展,但是1)看看是否可以在没有Lodash的情况下做到; 2)我认为将filter和forEach结合使用会更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM