0

My JSON is look like this

[ 

{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 ..then i want out on my HTML like

Title:cash to purchase

bag:3

Title:Offload

bag:9

title:onroad

bag:11

2
  • are you sure thats json? why is there =? Commented Mar 28, 2018 at 5:51
  • you can try out groupBy in lodash. documentation Commented Mar 28, 2018 at 5:57

2 Answers 2

2

Here you go. End to end Complete solution:

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) {
  if (!this[o.t_id]) {
    this[o.t_id] = {
      t_id: o.t_id,
      val1: 0,
      title: o.title
    };
    grouped.push(this[o.t_id]);
  }
  this[o.t_id].val1 += Number(o.val1);
}, Object.create(null));

console.log(grouped);
$scope.results = grouped;

Copy below code into your HTML:

<ion-content>
  <ion-list>
    <ion-item class="" ng-repeat="result in results">
        <div class="item item-text-wrap">
          Title: {{result.title}}
          <br>
          Bag: {{result.val1}}
        </div>
    </ion-item>
  </ion-list>
</ion-content>
Sign up to request clarification or add additional context in comments.

7 Comments

@Arjun Ramdas, did you check my answer?
yes..it's working...Is it is possible to divide the val1(ie sum) by total number of t_id..which means there are three t_id :'1' so i need to download the total sum(val1) by total t_id
Yes that’s also possible, will do it tomorrow. For the time being accept this answer and upvote because it’s currently fulfilling your requirements
it's very emergency to divide by total t_id by total sum(val1)....i already upvoted
it's okay...do if u can tomorrow..thanks Vic...you done a great job..if i get early answer then i can close my project...
|
1

Make a new object to store the sum of all the value w.r.t t_id as

var groupObj = {};
list.forEach( ele => 
    groupObj[ele.t_id] = groupObj[ele.t_id] ? 
          {title: ele.title, sum: groupObj[ele.t_id].sum + Number(ele.val1)} :
          {title: ele.title, sum: Number(ele.val1) }
);

This will make groupObj as

{
    1: { title: "cash to purchase", sum: 3 },
    2: { title: "onroad", sum: 9 },
    3: { title: "Onroad", sum: 11 }
}

Add comments if you need further help.

3 Comments

Is it possible to push the title and sum to an array?
b is the list.... updated.......also in array it wont be possible for you to find any t_id....you can make it but will have to iterate over it again and again.
let me know if need help to iterate over this object or need array code also.

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.