【问题标题】:merge value of json data合并json数据的值
【发布时间】:2016-05-19 23:10:21
【问题描述】:

我有一个 reactjs 应用程序,我需要使用 json 过滤数据,根据时间戳,我已经按年和月过滤了日期,现在我需要将其值汇总到月份中。我尝试了 lodash 一些功能,但成功了

这是我现在的 json 文件

'2016':{
  'jan':{
    '0':{
        'measures':{
        'step':{
          'text': 'step',
          'unit': 123
        },
        'calories':{
          'text': 'cal',
          'unit': 321
        }
      }
    },
    '1':{
      'measures':{
        'step':{
          'text': 'step',
          'unit': 45
        },
        'calories':{
          'text': 'cal',
          'unit': 65
        }
      }
    }
  },
  'feb':{
    '0':{
        'measures':{
        'step':{
          'text': 'step',
          'unit': 98
        },
        'calories':{
          'text': 'cal',
          'unit': 78
        }
      }
    },
    '1':{
      'measures':{
        'step':{
          'text': 'step',
          'unit': 21
        },
        'calories':{
          'text': 'cal',
          'unit': 41
        }
      }
    }
  }
}

我想要这个

'2016':{
  'jan':{
    'measures':{
      'step':{
        'text': 'step',
        'unit': 168 // sum value of measures
      },
      'calories':{
        'text': 'cal',
        'unit': 386
      }
     }
  },
  'feb':{
    'measures':{
      'step':{
        'text': 'step',
        'unit': 119
      },
      'calories':{
        'text': 'cal',
        'unit': 139
      }
    }
  }
}

有什么想法吗? reactjs上是否有任何有用的插件或组件 谢谢你的帮助

【问题讨论】:

    标签: json filter lodash


    【解决方案1】:

    您可以使用mapValues() 来遍历所有年份和月份。使用map()从地图中获取所有measuresreduce()设置每个度量类型的总和,然后thru()返回measures对象签名。

    var result = _.mapValues(years, months => {
      return _.mapValues(months, month => {
        return _.chain(month)
          .map()
          .map('measures')
          .reduce((measures, measureTypes) => {
            return _.reduce(measureTypes, (result, measureType, type) => {
              if (result[type]) {
                result[type].unit += measureType.unit;
              } else {
                result[type] = measureType;
              }
              return result;
            }, measures);
          }, {})
          .thru(measures => ({ measures }))
          .value();
      });
    });
    

    var years = {
      '2016': {
        'jan': {
          '0': {
            'measures': {
              'step': {
                'text': 'step',
                'unit': 123
              },
              'calories': {
                'text': 'cal',
                'unit': 321
              }
            }
          },
          '1': {
            'measures': {
              'step': {
                'text': 'step',
                'unit': 45
              },
              'calories': {
                'text': 'cal',
                'unit': 65
              }
            }
          }
        },
        'feb': {
          '0': {
            'measures': {
              'step': {
                'text': 'step',
                'unit': 98
              },
              'calories': {
                'text': 'cal',
                'unit': 78
              }
            }
          },
          '1': {
            'measures': {
              'step': {
                'text': 'step',
                'unit': 21
              },
              'calories': {
                'text': 'cal',
                'unit': 41
              }
            }
          }
        }
      }
    };
    
    var result = _.mapValues(years, months => {
      return _.mapValues(months, month => {
        return _.chain(month)
          .map()
          .map('measures')
          .reduce((measures, measureTypes) => {
            return _.reduce(measureTypes, (result, measureType, type) => {
              if (result[type]) {
                result[type].unit += measureType.unit;
              } else {
                result[type] = measureType;
              }
              return result;
            }, measures);
          }, {})
          .thru(measures => ({ measures }))
          .value();
      });
    });
    
    document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"&gt;&lt;/script&gt;

    【讨论】:

    • 谢谢,这非常有用,但是如果我有不同的 json 结构,link 每月过滤如何按照时间戳做同样的事情,我可以为这种情况做同样的逻辑吗?再次非常感谢
    • 所有measures 的时间戳是否相同?如果不是,那么我不完全确定结果应该是什么?您可能还应该提供转换后的结果值。
    • 好的,我已经将第一个与另一个具有更多值的 json 一起使用,并且正在使用来自 lodash 的 groupBy 来处理这种情况
    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2012-08-28
    相关资源
    最近更新 更多