【问题标题】:create a map out of list of objects using a key found in the object使用在对象中找到的键从对象列表中创建映射
【发布时间】:2013-07-22 09:06:39
【问题描述】:

基本上我有一个像这样的对象-

var data= [ 
{ id: 1,
objectType: 'Workstation',
isUp: true 
},
{ id: 2,
objectType: 'Workstation',
isUp: true 
},
{ id: 3,
objectType: 'Workstation',
isUp: false 
},
{ id: 4,
  objectType: 'Workstation',
  isUp: true 
},
{ id: 5,
  objectType: 'Workstation',
  isUp: false 
},
{ id: 6,
  objectType: 'Server',
  isUp: true 
},
{ id: 7,
  objectType: 'Server',
  isUp: true 
},
{ id: 8,
  objectType: 'Server',
  isUp: false 
},
{ id: 9,
  objectType: 'Server',
  isUp: false 
}
]

其中“isUp”是在线或离线对象状态。

我想把它转换成 -

{
'Workstation':{online_count:3, offline_count:2},
'Server':{online_count:2, offline_count:2}
}

任何帮助表示赞赏!

【问题讨论】:

    标签: javascript underscore.js javascript-objects


    【解决方案1】:

    我认为这样做可以:

    var result = {
        Workstation: {
            online_count: 0,
            offline_count: 0
        },
        Server: {
            online_count: 0,
            offline_count: 0
        }
    };
    data.forEach(function (item) {
        item.isUp ? result[item.objectType]['online_count']++ : result[item.objectType]['offline_count']++;
    });
    

    【讨论】:

      【解决方案2】:

      我为你准备了 dis 脚本:

      var data= [ 
          { id: 1,
          objectType: 'Workstation',
          isUp: true 
          },
          { id: 2,
          objectType: 'Workstation',
          isUp: true 
          },
          { id: 3,
          objectType: 'Workstation',
          isUp: false 
          },
          { id: 4,
            objectType: 'Workstation',
            isUp: true 
          },
          { id: 5,
            objectType: 'Workstation',
            isUp: false 
          },
          { id: 6,
            objectType: 'Server',
            isUp: true 
          },
          { id: 7,
            objectType: 'Server',
            isUp: true 
          },
          { id: 8,
            objectType: 'Server',
            isUp: false 
          },
          { id: 9,
            objectType: 'Server',
            isUp: false 
          }
          ]
      var finalData = new Array();
      data.forEach(function (item) {
          var found = false;
          for (var i = 0; i < finalData.length; i++) {
              if (finalData[i].objType == item.objectType) {
                  if (item.isUp)
                      finalData[i].online_count++;
                  else
                      finalData[i].offline_count++;
                  found = true;
              }
          }
          if (!found) {
              var newObj = new Object();
              newObj.objType = item.objectType;
              newObj.online_count = item.isUp ? 1 : 0;
              newObj.offline_count = item.isUp ? 0 : 1;        
              finalData.push(newObj);
          }    
      });
      console.log(finalData);
      

      【讨论】:

        猜你喜欢
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多