js數組工具處理之合併相同字段分組

話不多說直接上碼:

以下是根據time進行分組的

var tempList = [
      { userImage: '', userName: '李測試', userWork: '前端', iPhone: '18516776220', time: '2017-11-17',company:"上海測試一公司"},
      { userImage: '', userName: '劉測試', userWork: '前端', iPhone: '18511111111', time: '2017-11-18', company: "上海測試二公司"},
      { userImage: '', userName: '黃一明', userWork: '前端', iPhone: '18588888888', time: '2017-11-18', company: "上海測試三公司"},
      { userImage: '', userName: '黃大明', userWork: '前端', iPhone: '18588888888', time: '2017-11-18', company: "上海測試五公司" },
      { userImage: '', userName: '董測試', userWork: '法制特', iPhone: '18522222222', time: '2017-11-20', company: "上海測試二公司"}
    ];

    var map = {},
        dest = [];
    for (var i = 0; i < tempList.length; i++) {
      var ai = tempList[i];
      if (!map[ai.time]) {
        dest.push({
          initial: ai.time,
          busInfoList: [ai]
        });
        map[ai.time] = ai;
      } else {
        for (var j = 0; j < dest.length; j++) {
          var dj = dest[j];
          if (dj.initial == ai.time) {
            dj.busInfoList.push(ai);
            break;
          }
        }
      }
    }
     console.log(dest);
以上代碼經過驗證,“假一賠十”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章