微信小程序二维数组分组

// data二维数组
// name 按该字段的值分组
fenzu(data,name){
     
    let that = this
    let arr = []
    for(var i=0;i<data.length;i++){
     
      arr.push(data[i].address)
    }
    let arrs = that.quchong(arr)
    let datas = []
    
    for(var i=0;i<arrs.length;i++){
     
      let item = []
      item.push(arrs[i])
      for(var j=0;j<data.length;j++){
     
        if(data[j][name] == arrs[i]){
     
          item.push(data[j])
        }
      }
      datas.push(item)
    }
    return datas
  },
  quchong(arr){
     
    console.log(arr)
    var tempArr = []; //返回的值
    var indexArr= []; //返回的索引
      for (var i = 0; i < arr.length; i++) {
     
        if (tempArr.indexOf(arr[i]) == -1) {
     
          tempArr.push(arr[i]);
          indexArr.push(i)
        } else if (tempArr.indexOf(arr[i])>=0){
     
          for(var j = 0;j<tempArr.length;j++){
     
          if (tempArr[j] == arr[i]) {
     
            tempArr.splice(j, 1, tempArr[j]);
            indexArr.splice(j, 1, i);
          }
        }
      }
    }
    return tempArr
  },

你可能感兴趣的:(小程序,js,小程序,js)