代码之家  ›  专栏  ›  技术社区  ›  Arjun Ramdas

使用sum对JavaScript数组分组

  •  -3
  • Arjun Ramdas  · 技术社区  · 8 年前
    [ 
    
    {t_id :"1",  val1 : "1" ,   title:"cash to purchase",   unit :"bag"},
    
    {t_id :"1"  ,val1 : "1" ,  title:"cash to purchase", unit :"bag"}
    
    {t_id :"1",val1 : "1" , title:"cash to purchase", unit :"bag"}
    
    {t_id :"2",val1 : "4" , title:"offload", unit :"bag"},
    
    {t_id :"2",val1 : "5" , title:"onroad", unit :"bag"},
    
    {t_id :"3", val1 : "5" , title:"Onroad", unit :"bag"},
    
    {t_id :"3", val1 : "6" , title:"Onroad", unit :"bag"},
    
    ]
    

    我想按t\U id分组,根据每个t\U id找到val1的和,并将val1除以t\U id的总数。。。。例如:数组中有三个t-id 1,三个t\U id的和为3(val1)。。因此val1必须是总t\U id的3(即3/3)。。。

    然后我想在我的HTML上

    标题:购买现金

    包:1(即(3/3)

    标题:卸载

    袋子:4.5(即9/2)

    标题:onroad

    包:5.5(即11/2)。。。WARE 2是总t\U id

    2 回复  |  直到 8 年前
        1
  •  1
  •   Vikasdeep Singh    8 年前

    这样做:

    var array = [{t_id:"1",val1:"1",title:"cash to purchase",unit:"bag"},{t_id:"1",val1:"1",title:"cash to purchase",unit:"bag"},{t_id:"1",val1:"1",title:"cash to purchase",unit:"bag"},{t_id:"2",val1:"4",title:"offload",unit:"bag"},{t_id:"2",val1:"5",title:"onroad",unit:"bag"},{t_id:"3",val1:"5",title:"Onroad",unit:"bag"},{t_id:"3",val1:"6",title:"Onroad",unit:"bag"}];
    
    
    var grouped = [];
    
    array.forEach(function(o) {
      var count = 0;
      if (!this[o.t_id]) {
        this[o.t_id] = {
          t_id: o.t_id,
          val1: 0,
          title: o.title,
          counter: count
        };
        grouped.push(this[o.t_id]);
      }
      this[o.t_id].val1 += Number(o.val1);
      this[o.t_id].counter += Number(++count);
    }, Object.create(null));
    
    console.log(grouped);

    现在在HTML中显示如下: Bag: {{result.val1/result.counter}}

        2
  •  1
  •   Tomero Indonesia    8 年前

    我不知道下面的脚本是否适合您,但输出是您想要的。

    var sources = [
    {t_id: "1", val1: "1", title: "cash to purchase", unit: "bag"},
    {t_id: "1", val1: "1", title: "cash to purchase", unit: "bag"},
    {t_id: "1", val1: "1", title: "cash to purchase", unit: "bag"},
    {t_id: "2", val1: "4", title:"offload", unit :"bag"},
    {t_id: "2", val1: "5", title:"onroad", unit :"bag"},
    {t_id: "3", val1: "5", title:"Onroad", unit :"bag"},
    {t_id: "3", val1: "6", title:"Onroad", unit :"bag"}
    ];
    
    var groups = {};
    for(var i in sources){
    	var source = sources[i];
    	if(typeof groups[source.t_id] !== 'undefined'){
        	groups[source.t_id].sum_of_val1 += Number(source.val1);
        	groups[source.t_id].num_of_items += 1;
        }else{
        	groups[source.t_id] = source;
            groups[source.t_id].sum_of_val1 = Number(source.val1);
        	groups[source.t_id].num_of_items = 1;
        }
    }
    
    // print out put
    for(var i in groups){
    	var group = groups[i];
    	
        document.write("Title: " + group.title + "<br/>Bag: " + (group.sum_of_val1 / group.num_of_items) + " (ie.: " + group.sum_of_val1 + " / " + group.num_of_items+ ")<br/><br/>");
    }