Lodash - 如何从嵌套的json中一次只选择一项?

Posted

技术标签:

【中文标题】Lodash - 如何从嵌套的json中一次只选择一项?【英文标题】:Lodash - how to pick only one item at a time from nested 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"
        
    ]

【问题讨论】:

【参考方案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>

【讨论】:

以上是关于Lodash - 如何从嵌套的json中一次只选择一项?的主要内容,如果未能解决你的问题,请参考以下文章

想拿起工具或枪支,但想在统一 c# 中一次只选择一个

如何在 Flutter 中一次只在屏幕上显示一个表单域?

UITableview如何一次只显示一个单元格

如何从表中一次选择相等的值[重复]

如何从 R 中的 selectInput 函数中一次选择所有输入

在反应jsx中一次遍历一个数组