-2

Hi I want to do sum of the quantity for each reason name.And I have 2 arrays as per below.

var allreasonsids=[];
 allreasonsids = [
   {reasonid: 1, reasonname: abc}, 
   {reasonid: 2, reasonname: def}, 
   {reasonid: 3, reasonname: ghi}, 
   {reasonid: 4, reasonname: jkl}
];


var reasonsandcount=[];
reasonsandcount=[
{reasonid: 1, quantity: 5},
{reasonid: 2, quantity: 10},
{reasonid: 1, quantity: 3},
{reasonid: 3, quantity: 4},
{reasonid: 1, quantity: 2},
{reasonid: 2, quantity: 6}
];

I want the result as per below:

Output: abc :10

def:16

ghi:4

jkl:0

Please suggest me any answer. Thank You.

2
  • use basic javascipt or linqjs.codeplex.com Commented Jan 9, 2017 at 12:59
  • @lordkain Hi I am using basic javascript. can u please tell me solution for this? Commented Jan 9, 2017 at 13:05

2 Answers 2

2

You can use reduce() and filter() to return object as result.

var allreasonsids = [
  {reasonid: 1, reasonname: 'abc'}, 
  {reasonid: 2, reasonname: 'def'}, 
  {reasonid: 3, reasonname: 'ghi'}, 
  {reasonid: 4, reasonname: 'jkl'}
];

var reasonsandcount=[
  {reasonid: 1, quantity: 5},
  {reasonid: 2, quantity: 10},
  {reasonid: 1, quantity: 3},
  {reasonid: 3, quantity: 4},
  {reasonid: 1, quantity: 2},
  {reasonid: 2, quantity: 6}
];

var result = allreasonsids.reduce(function(r, e) {
  var quant = reasonsandcount.filter(function(a) {
    return e.reasonid == a.reasonid
  }).reduce(function(re, el) {
    return re + el.quantity
  }, 0)
  r[e.reasonname] = quant;
  return r
}, {})

console.log(result)

You can also just use reduce() and forEach() loop.

var allreasonsids = [
  {reasonid: 1, reasonname: 'abc'}, 
  {reasonid: 2, reasonname: 'def'}, 
  {reasonid: 3, reasonname: 'ghi'}, 
  {reasonid: 4, reasonname: 'jkl'}
];

var reasonsandcount=[
  {reasonid: 1, quantity: 5},
  {reasonid: 2, quantity: 10},
  {reasonid: 1, quantity: 3},
  {reasonid: 3, quantity: 4},
  {reasonid: 1, quantity: 2},
  {reasonid: 2, quantity: 6}
];

var result = allreasonsids.reduce(function(r, e) {
 var quant = 0;
 reasonsandcount.forEach(el => el.reasonid == e.reasonid ? quant+=el.quantity : 0);
 r[e.reasonname] = quant;
 return r
}, {})

console.log(result)

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

3 Comments

thanx a lot for helping me.But in actual I have reason name with space e.g.New Stock.so I cannot access the value in result object with result.New Stock .Can you please help me out?If any confusion in this then please ask me
,If the result is in any list or array then also it is good for me.if that will be possible then can you give me result in that?
I get What I want thanx for your time.
1

The solution using Array.prototype.reduce, Object.keys and Array.prototype.forEach functions:

var allreasonsids = [{reasonid: 1, reasonname: 'abc'}, {reasonid: 2, reasonname: 'def'}, {reasonid: 3, reasonname: 'ghi'}, {reasonid: 4, reasonname: 'jkl'}],
    reasonsandcount = [{reasonid: 1, quantity: 5},{reasonid: 2, quantity: 10},{reasonid: 1, quantity: 3},{reasonid: 3, quantity: 4},{reasonid: 1, quantity: 2},{reasonid: 2, quantity: 6}];

// getting sums for grouped `reasonid` items
var sums = reasonsandcount.reduce(function (a, b) {
  (a[b.reasonid]) ? a[b.reasonid] += b.quantity : a[b.reasonid] = b.quantity;
  return a;
}, {});

var result = {}, keys = Object.keys(sums);
allreasonsids.forEach(function (o) {
    this[o.reasonname] = (keys.indexOf(o.reasonid + "") !== -1)? sums[o.reasonid] : 0;
}, result);

console.log(result);

3 Comments

Thank you for help.But in actual I have reason name with space e.g.New Stock instead of abc.so I cannot access the value in result object with result.New Stock .Can you please help me.
Hi RomanPerekhrest, sums variable is so helpful to me.With some little extra effort I am able to get what I want.Thanx again.
@chhaya_patel, glad to help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.