根据json的某一字段的值相同重组数组

    this.list = resp.result;
    var map = {},
      dest = [];
    for (var i = 0; i < this.list.media.length; i++) {
      if (!map[this.list.media[i].media_create_time]) {
        if (this.list.media[i].media_type == 1) {
          this.id++;
          this.list.media[i].id = this.id;
        }
        dest.push({
          media_create_time: this.list.media[i].media_create_time,
          media_type: this.list.media[i].media_type,
          data: [this.list.media[i]]
        });
        map[this.list.media[i].media_create_time] = this.list.media[i];
      } else {
        for (var j = 0; j < dest.length; j++) {
          var dj = dest[j];
          if (
            dj.media_create_time == this.list.media[i].media_create_time
          ) {
            if (this.list.media[i].media_type == 1) {
              this.id++;
              this.list.media[i].id = this.id;
            }
            dj.data.push(this.list.media[i]);
            break;
          }
        }
      }
    }
    this.newlist = dest;
    console.log(dest);

你可能感兴趣的:(vue)