0

I have a requirement where i need to find sum the values of amt in different objects having same name. Below is the code snippet

traveler = [
  {  description: 'Senior', Amount: 50},
   {  description: 'Senior', Amount: 50},
   {  description: 'Adult', Amount: 75},
   {  description: 'Child', Amount: 35},
   {  description: 'Infant', Amount: 25 },
];

Here i want to caluclate the total sum of Amount in different objects with same description. eg: object 0 and 1 contains same description 'Senior' so the total amt is 100

How to achieve this in angular2?

Should i use inner forloops or is there any better approach? Please help me

4
  • 2
    Possible duplicate of Sorting an array of JavaScript objects Commented Jul 10, 2018 at 13:59
  • 2
    if it is an array, then it has no written indices or if it is an object, then ith sould have curly brackets instead of square brackets. Commented Jul 10, 2018 at 14:00
  • traveler doesn't seem like a valid array Commented Jul 10, 2018 at 14:01
  • 1
    Please update your question to show what you have already tried, showcasing a specific problem you are facing in a minimal, complete, and verifiable example. Commented Jul 10, 2018 at 14:02

6 Answers 6

2

You could take a Map for collecting the values and render an array of objects with the grouped result.

var traveler = [{ description: 'Senior', amount: 50 }, { description: 'Senior', amount: 50 }, { description: 'Adult', amount: 75 }, { description: 'Child', amount: 35 },  { description: 'Infant', amount: 25 }],
    grouped = Array.from(
        traveler.reduce(
            (m, { description, amount }) => m.set(description, (m.get(description) || 0) + amount),
            new Map
        ).entries(),
        ([description, amount]) => ({ description, amount })
    );
    
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

You can use reduce to group the array into an object.

let traveler = [{"description":"Senior","Amount":50},{"description":"Senior","Amount":50},{"description":"Adult","Amount":75},{"description":"Child","Amount":35},{"description":"Infant","Amount":25}]

let result = traveler.reduce((c, v) => {
  c[v.description] = (c[v.description] || 0) + v.Amount;
  return c;
}, {});

console.log(result);

Comments

0

I think you should use array#reduce method to do something like this perhaps:

let traveler = [
       {  description: 'Senior', Amount: 50},
       {  description: 'Senior', Amount: 50},
       {  description: 'Adult', Amount: 75},
       {  description: 'Child', Amount: 35},
       {  description: 'Infant', Amount: 25 },
    ];
    
function sumFinder(description) {
 return traveler.reduce((sum, e) => {
   (e.description == description) ? (sum += e.Amount) : (sum += 0)
   return sum;
 }, 0);
}

console.log(sumFinder('Senior'));

Comments

0

Use Array.reduce() to get this output:

var traveler = [
   {  description: 'Senior', Amount: 50},
   {  description: 'Senior', Amount: 50},
   {  description: 'Adult', Amount: 75},
   {  description: 'Child', Amount: 35},
   {  description: 'Infant', Amount: 25 },
];
var res = traveler.reduce((acc, obj)=>{
  var existItem = acc.find(item => item.description === obj.description);
  if(existItem){
    existItem.Amount += obj.Amount;
    return acc;
  } 
  acc.push(obj);
  return acc;
}, []);
console.log(res);

Comments

0

You can do it using rxjs among others:

travelers = [
      {  description: 'Senior', amount: 50},
      {  description: 'Senior', amount: 50},
      {  description: 'Adult', amount: 75},
      {  description: 'Child', amount: 35},
      {  description: 'Infant', amount: 25 }
    ];
//emit each person
const source = Rx.Observable.from(travelers);
//group by description
const example = source
  .groupBy(traveler => traveler.description)
  //return each item in group as array
  .mergeMap(group => group.toArray())
const reducer = (accumulator, currentValue) => {return accumulator + currentValue.amount};
const subscribe = example.subscribe(
  val => 
    console.log(
      {
        description: val[0].description, 
        amount: val.reduce( reducer, 0 )
      }
    )
);

This code's output is what you want:

[object Object] {
  amount: 100,
  description: "Senior"
}
[object Object] {
  amount: 75,
  description: "Adult"
}
[object Object] {
  amount: 35,
  description: "Child"
}
[object Object] {
  amount: 25,
  description: "Infant"
}

Finally, here is a JSBin you can play with: http://jsbin.com/kacoqulike/1/edit?js,console

Comments

0

A clean and simple solution with Array.prototype.reduce and ES6 Object assignment desctructuring:

const traveler = [{"description":"Senior","Amount":50},{"description":"Senior","Amount":50},{"description":"Adult","Amount":75},{"description":"Child","Amount":35},{"description":"Infant","Amount":25}]

const result = traveler.reduce((all, {description: d, Amount: a}) => {

    all[d] = (all[d] || 0) + a;
    return all;

}, {});

console.log(result);

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.