【问题标题】:Lodash - how to pick only one item at a time from nested json?Lodash - 如何从嵌套的json中一次只选择一项?
【发布时间】:2020-11-24 09:30:00
【问题描述】:

我有一个嵌套的天气 json 数据:

{
    "Temp": [{
            "time": "2020-08-04T12:00:00Z",
            "value": "12"
        },

        {
            "time": "2020-08-04T13:00:00Z",
            "value": "13"
        }
    ],
    "Humidity": [{
            "time": "2020-08-04T12:00:00Z",
            "value": "70"
        },

        {
            "time": "2020-08-04T13:00:00Z",
            "value": "73"
        }
    ]
}

现在(使用 Lodash 或任何其他建议)面临的挑战是以某种方式按时间对它们进行分组,并且一次只选择一个项目,例如:

{
    "data": [{
            "time": "2020-08-04T12:00:00Z",
            "Temprature": "12",
            "Humidity": "70"
        },
        {
            "time": "2020-08-04T13:00:00Z",
            "Temprature": "13",
            "Humidity": "73"
        }
    ]
}

【问题讨论】:

    标签: javascript json reactjs object lodash


    【解决方案1】:

    查看Object.entries()Array.prototype.reduce()for...of 了解更多信息。

    // Input.
    const input = {
      "temperature": [
        {"time": "2020-08-04T12:00:00Z", "value": "12"}, 
        {"time": "2020-08-04T13:00:00Z", "value": "13"}
      ],
      "humidity": [
        {"time": "2020-08-04T12:00:00Z", "value": "70"},
        {"time": "2020-08-04T13:00:00Z", "value": "73"}
      ]
    }
    
    // Zip Using Time.
    const zipUsingTime = x => Object.entries(Object.entries(x).reduce((acc, [key, values], index) => {
      
      // Unpack Values.
      for (const y of values) {
        const {time, value} = y
        acc[time] = {...acc[time], [key]: value}
      }
    
      // ..
      return acc
    
    }, {})).map(([time, props]) => ({time, ...props}))
    
    // Output.
    const output = {
      data: zipUsingTime(input)
    }
    
    // Proof.
    console.log(output)

    【讨论】:

      【解决方案2】:

      const input={"Temp":[{"time":"2020-08-04T12:00:00Z","value":"12"},{"time":"2020-08-04T13:00:00Z","value":"13"}],"Humidity":[{"time":"2020-08-04T12:00:00Z","value":"70"},{"time":"2020-08-04T13:00:00Z","value":"73"}]}
      
      const data = input.Temp.map(
         ({time, value: Temprature}) => ({
           time, 
           Temprature, 
           Humidity: input.Humidity.find(h => h.time === time).value
          }
      ));
      
      console.log(data);

      【讨论】:

        【解决方案3】:

        使用lodash 你可以这样做。

        const data = {
            "Temp": [{
                    "time": "2020-08-04T12:00:00Z",
                    "value": "12"
                },
        
                {
                    "time": "2020-08-04T13:00:00Z",
                    "value": "13"
                }
            ],
            "Humidity": [{
                    "time": "2020-08-04T12:00:00Z",
                    "value": "70"
                },
        
                {
                    "time": "2020-08-04T13:00:00Z",
                    "value": "73"
                }
            ]
        };
        
        // Helper function to format an object
        const formatObject = (key) => ({ time, value }) => ({ time, [key]: value });
        
        const getData = () => {
            
            // Start a chain for the data object
            return _.chain(data)
                // Get the object entries
                .entries()
                // Map each key (eg. Temp) to its value
                // Spread operator used to produce the final array with depth 1
                .reduce((acc, [key, values]) => [...acc, ...values.map(formatObject(key))], [])
                // Group the data by time into an object
                .reduce((acc, val) => _.set(acc, val.time, _.merge(acc[val.time], val)), {})
                // Get the values
                .values()
                // Unwrap the value
                .value();
        };
        
        const result = {
            data: getData(),
        };
        
        console.log(result);
        <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>

        【讨论】:

          猜你喜欢
          • 2018-10-25
          • 1970-01-01
          • 2021-06-26
          • 1970-01-01
          • 2017-01-31
          • 1970-01-01
          • 2011-06-12
          • 2021-02-02
          相关资源
          最近更新 更多