-3
[ 

{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"},

]

I want to group by t_id and find the sum of val1 according to each t_id and divide the val1 by total number of t_id .... eg: there are three t-id 1 in the array and sum is of three t_id is 3(val1)..so the val1 must be 3/total t_id( ie 3/3)...

then i want out on my HTML like

Title:cash to purchase

bag:1 (ie(3/3)

Title:Offload

bag:4.5 (ie 9/2)

title:onroad

bag:5.5 (ie 11/2) ...were 2 is total t_id

1
  • Add JavaScript code as well here Commented Mar 29, 2018 at 4:01

2 Answers 2

1

Do it like this:

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);

Now in HTML show like this : Bag: {{result.val1/result.counter}}

Sign up to request clarification or add additional context in comments.

5 Comments

Not exactly..please divide the val1 with counter that is ....in t_id 1 val1 must be 3...(val1/totalnumberof t_id)...
@Arjun Ramdas, Did you check my answer?
Here counter is number of t_id. t_id : 1 was 3 times so counter ran for 3 times. This will work
@ArjunRamdas, great. Please upvote, if not done yet.
@ArjunRamdas, one more thing, please study a little about how to use Stackoverflow i.e. how to ask questions and in question always add your efforts or whatever research you have done. Going forward in future professional life as well, Stackoverflow will help you a lot.
1

I dont know if script below is right for you, but the output it was like you want.

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/>");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.