代码之家  ›  专栏  ›  技术社区  ›  Dhiraj

根据数组的键对数组进行分组,并使用组作为索引创建新数组

  •  0
  • Dhiraj  · 技术社区  · 8 年前

    我试图从一个数组中创建一个新数组,该数组将按键分组,新数组的索引将是旧数组的键,如下所示

    旧阵列

    Array1
    0 :term_id: "1"
       capacity:"11"
       price: "452"
    1 :term_id: "2"
       capacity:"33"
       price: "44"
    2 :term_id: "1"
       capacity:"1"
       price: "2"
    

    我希望将此阵列按 term_id 因此,新阵列是

    新建阵列

    Array2
    1 :terms >
       0: capacity:"11"
          price: "452"
       1: capacity:"1"
          price: "2"
    2 :terms >
      0:  capacity:"33"
          price: "44"
    

    如您所见,在新数组中,数组按term\u id的索引分组,其中还有另一个arary及其内容

    我试过使用 groupBy 方法,但我无法保留旧数组的索引

    5 回复  |  直到 8 年前
        1
  •  1
  •   Ele    8 年前

    您可以使用该功能 reduce 分组并生成所需的输出。

    var Array1 = [{  term_id: "1",  capacity: "11",  price: "452"}, {  term_id: "2",  capacity: "33",  price: "44"}, {  term_id: "1",  capacity: "1",  price: "2"}];
    
    var result = Object.values(Array1.reduce((a, {term_id, capacity, price}) => {
      (a[term_id] || (a[term_id] = {terms: []})).terms.push({capacity, price});
      return a;
    }, {}));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        2
  •  1
  •   Hassan Imam Ravi kant    8 年前

    您可以使用 array#reduce 根据对对象数组进行分组的步骤 term_id 在对象中,然后使用 Object.values()

    var data = [{ term_id: "1", capacity: "11", price: "452" }, { term_id: "2", capacity: "33", price: "44" }, { term_id: "1", capacity: "1", price: "2" }],
        result = Object.values(data.reduce((r,{term_id, capacity, price})=> {
          r[term_id] = r[term_id] || [];
          r[term_id].push({capacity, price});
          return r;
        },{}));
    console.log(result);
        3
  •  1
  •   yajiv    8 年前

    您可以使用 reduce 数组的。原型

    x = [{term_id:"1",capacity:"11",price:"452"},{term_id:"2",capacity:"33",price:"44"},{term_id:"1",capacity:"1",price:"2"}];
    
    y = x.reduce((n, e) => {
                var clone = Object.assign({}, e);  //creating duplicate object
                delete clone.term_id;              //deleteing term_id property from clone object
                if (!n[e.term_id]) {
                    n[e.term_id] = [];
                }
                n[e.term_id].push(clone);    //pushing clone instead of e as e contains term_id which we don't need in result
                return n;
            }, {});
       
    console.log(y);
        4
  •  0
  •   kpie    8 年前

    如果您想使用循环。。。将所有数据复制到结果中有点浪费内存,我认为更好的做法是生成所需的数据并保留任何已经存在的值,但这可能会导致更糟糕的代码-_-

    function idxOfGroupBy(hl,key){
    let ret = [];
    for(var k = 0; k < hl.length; k++){
    	try{
    		ret[hl[k][key]].push(k);
    	}catch(err){
    		ret[hl[k][key]] = [];
    		ret[hl[k][key]].push(k);
    	}
    }
    return(ret);
    }
    
    let hashList = [{at1:"1",atr2:"."},{at1:"2",atr2:".."},{at1:"1",atr2:"..."}];
    let groupIdxs = idxOfGroupBy(hashList,"at1");
    console.log(groupIdxs);
        5
  •  0
  •   Jonas Wilms    8 年前

    只需使用哈希表进行分组:

     const result = [], hash = {};
    
     for(const obj of array) {
       if(hash[obj.term_id]) {
         hash[obj.term_id].push(obj);
       } else {
         result.push(hash[obj.term_id] = [obj]);
       }
     }