【问题标题】:Convert an array of objects to another array of objects using jQuery使用 jQuery 将一个对象数组转换为另一个对象数组
【发布时间】:2018-05-08 02:28:05
【问题描述】:

我在我的 JavaScript 文件中得到了一个结果,我想将它转换成另一个对象。

我原来的结果:

 results = [{Key1:'Value1', Key2:100, Key3:'A'},
            {Key1:'Value1', Key2:40,  Key3:'B'},
            {Key1:'Value2', Key2:60,  Key3:'A'},
            {Key1:'Value2', Key2:70,  Key3:'B'},
            {Key1:'Value3', Key2:50,  Key3:'A'},
            {Key1:'Value3', Key2:90,  Key3:'B'}];

使用 jQuery 或 JavaScript 将其转换为看起来像一个对象数组。我怎样才能做到这一点?

finalResult=[{Key1:'Value1', A:100, B:40},
             {Key1:'Value2', A:60,  B:70},
             {Key1:'Value3', A:50,  B:90}];

【问题讨论】:

  • 好吧,Stackoverflow 不是免费的代码编写服务,因此您可以向我们展示您的尝试,以便我们帮助您修复您的代码
  • 为什么用(&)而不是[&]包围对象
  • @brk 因为他不知道数组字面量的样子

标签: javascript jquery arrays object


【解决方案1】:

您可以使用array#reduce 根据Key1 值对对象进行分组。然后使用Object.values(),就可以得到每个key的累加结果。

const input = [{Key1:'Value1',Key2:100,Key3:'A'},{Key1:'Value1',Key2:40,Key3:'B'},{Key1:'Value2',Key2:60,Key3:'A'},{Key1:'Value2',Key2:70,Key3:'B'},{Key1:'Value3',Key2:50,Key3:'A'},{Key1:'Value3',Key2:90,Key3:'B'}];

const result = input.reduce((r, {Key1, Key2, Key3}) => {
  r[Key1] ?
      r[Key1][Key3] = Key2
    : r[Key1] = {Key1, [Key3] : Key2 };
  return r;
},{});
console.log(Object.values(result));

【讨论】:

    【解决方案2】:

    使用这个:

    let results=[{Key1:'Value1',Key2:100,Key3:'A'},
             {Key1:'Value1',Key2:40,Key3:'B'},
             {Key1:'Value2',Key2:60,Key3:'A'},
             {Key1:'Value2',Key2:70,Key3:'B'},
             {Key1:'Value3',Key2:50,Key3:'A'},
             {Key1:'Value3',Key2:90,Key3:'B'}];
             
    let finalResult = [];
    for (let original of results) {  
        if (!finalResult[original.Key1]) {
            finalResult[original.Key1] = { Key1: original.Key1 };
        }
    
        finalResult[original.Key1][original.Key3] = original.Key2;
    }
    
    console.log(finalResult);
    

    【讨论】:

      【解决方案3】:

      你初始化数组不正确,需要把外面的()替换成[],比如

      var results = [ {}, {} ];
      

      你需要先制作一张不同Key1的地图

      var map = {};
      results.forEach( function(item){
         map[ item.Key1 ] = map[ item.Key1 ] || {};
         map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
         map[ item.Key1 ][ item.Key3 ] += item.Key2;
      })
      

      现在迭代这个地图的键,并准备你的输出

      var output = Object.keys(map).map( function(value){
          var obj = { key1 : value };
          Object.keys( map[ value ] ).forEach( function(key2){
              obj[ key2 ] = map[ value ][ key2 ]
          });
          return obj;
      });
      

      演示

      var results=[
        {Key1:'Value1',Key2:100,Key3:'A'},
        {Key1:'Value1',Key2:40,Key3:'B'},
        {Key1:'Value2',Key2:60,Key3:'A'},
        {Key1:'Value2',Key2:70,Key3:'B'},
        {Key1:'Value3',Key2:50,Key3:'A'},
        {Key1:'Value3',Key2:90,Key3:'B'}
      ];
      var map = {};
      results.forEach( function(item){
         map[ item.Key1 ] = map[ item.Key1 ] || {};
         map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
         map[ item.Key1 ][ item.Key3 ] += item.Key2;
      });
      
      var output = Object.keys(map).map( function(value){
          var obj = { key1 : value };
          Object.keys( map[ value ] ).forEach( function(key2){
              obj[ key2 ] = map[ value ][ key2 ]
          });
          return obj;
      });
      console.log( output );

      【讨论】:

        猜你喜欢
        • 2021-12-13
        • 2017-07-17
        • 1970-01-01
        • 2019-10-20
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多