根据数组的某一字段重新分组

  var map = {},
    dest = [];
  let custody_list = [
    {
      media_create_time: 123,
      media_type: 5,
      car: 456
    },
    {
      media_create_time: 456,
      media_type: 6,
      car: 489
    },
    {
      media_create_time: 123,
      media_type: 7,
      car: 466
    }
  ];
  for (var i = 0; i < custody_list.length; i++) {
    if (!map[custody_list[i].media_create_time]) {
      dest.push({
        media_create_time: custody_list[i].media_create_time,
        media_type: custody_list[i].media_type,
        data: [custody_list[i]]
      });
       map[custody_list[i].media_create_time] = custody_list[i];
    } else {
      for (var j = 0; j < dest.length; j++) {
        var dj = dest[j];
        if (dj.media_create_time == custody_list[i].media_create_time) {
          dj.data.push(custody_list[i]);
          break;
        }
      }
    }
  }
  console.log(dest, "重组的数组");

你可能感兴趣的:(分组)