如果其中一个键相同,则在数组下收集数组元素

时间:2018-09-19 11:55:59

标签: javascript json

我有一个数组来收集像

这样的对象
myArray = [ 
           { lid_id: "1_1", et: "car", ty: "STS", dc: null, c: null, cp: null, im: false},
           { lid_id: "1_1", et: "car", ty: "STS", dc: 7, c: 11333, cp: 60, im: true},
           { lid_id: "1_2", et: "car", ty: "STC", dc: null, c: null, cp: null, im: false},
           { lid_id: "10_14473", et: "truck", ty: null, dc: 7, c: 14869, cp: 40, im: true}
          ]

如果dc, c, cp具有相同的值,我想构建新对象并冲突imlid_id键。例如,以下具有键值"lid_id":"1_1"的对象收集ce下的元素。如果lid_id相同,如何收集这些元素?

myArray.forEach(function(data){
    "body": { "lid_id": "1_1",
              "et": "car", 
              "ty": "STS",
              "ce": [ 
                      { 
                        "dc": null, 
                        "c": null, 
                        "cp": null, 
                        "im": false
                      },
                      {
                        "dc": 7, 
                        "c": 11333, 
                        "cp": 60, 
                        "im": true
                      } 
                    ]
            },
     "body": { "lid_id": "1_2",
              "et": "car", 
              "ty": "STC",
              "ce": [ 
                      { 
                        "dc": null, 
                        "c": null, 
                        "cp": null, 
                        "im": false
                      }
                    ]
            },
     "body": { "lid_id": "10_14473",
               ...............
             }
})

2 个答案:

答案 0 :(得分:2)

我的方法是使用reduce来实现输出。

const myArray = [{
    lid_id: "1_1",
    et: "car",
    ty: "STS",
    dc: null,
    c: null,
    cp: null,
    im: false
  },
  {
    lid_id: "1_1",
    et: "car",
    ty: "STS",
    dc: 7,
    c: 11333,
    cp: 60,
    im: true
  },
  {
    lid_id: "1_2",
    et: "car",
    ty: "STC",
    dc: null,
    c: null,
    cp: null,
    im: false
  },
  {
    lid_id: "10_14473",
    et: "truck",
    ty: null,
    dc: 7,
    c: 14869,
    cp: 40,
    im: true
  }
];

const grouping = myArray.reduce((acc, curr) => {
  const foundIndex = acc.findIndex(a => a.lid_id === curr.lid_id);
  const {
    dc,
    c,
    cp,
    im,
    lid_id,
    et,
    ty
  } = curr;
  const ceItem = {
    dc,
    c,
    cp,
    im
  };
  
  if (foundIndex > -1) {
    acc[foundIndex].ce.push(ceItem);
    return acc;
  }

  return [
    ...acc,
    {
      lid_id,
      et,
      ty,
      ce: [
        ceItem
      ]
    }
  ]
}, []);

console.log(grouping);

答案 1 :(得分:1)

第一步是按ID将数据预过滤到包含子条目数组的映射中:

let myArray = [ 
           { lid_id: "1_1", et: "car", ty: "STS", dc: null, c: null, cp: null, im: false},
           { lid_id: "1_1", et: "car", ty: "STS", dc: 7, c: 11333, cp: 60, im: true},
           { lid_id: "1_2", et: "car", ty: "STC", dc: null, c: null, cp: null, im: false},
           { lid_id: "10_14473", et: "truck", ty: null, dc: 7, c: 14869, cp: 40, im: true}
          ];

console.log(myArray);

let myMap = [[]];

myArray.forEach( e =>
    {
            let internalArray = myMap[e.lid_id];
      console.log("internal: " + internalArray);

      if(internalArray == undefined){
         myMap[e.lid_id] = [];      
      }

            myMap[e.lid_id].push(e);

    }
);

console.log(myMap);

在那之后,很简单地进行一些进一步的处理以获得适当的分组。