0
    [ 
        { Phase: "Phase 1",Value1: "5", Value2: "4" },
        { Phase: "Phase 1",Value1: "3", Value2: "7" },
        { Phase: "Phase 1",Value1: "5", Value2: "2" },
        { Phase: "Phase 1",Value1: "2", Value2: "1" },
        { Phase: "Phase 2",Value1: "1", Value2: "3" },
        { Phase: "Phase 2",Value1: "4", Value2: "8" },
        { Phase: "Phase 2",Value1: "5", Value2: "1" },
        { Phase: "Phase 2",Value1: "1", Value2: "1" }
    ]

Hi, i have a this Array of objects and i am expecting the below format. I wants to get result by adding all data for value1, value2 (It can be dynamic or may be more) Grouping by Phase.

I am coding in javascript but, result is not coming proper.

    [ 
        { Phase: "Phase 1",Value1: "15", Value2: "14" },
        { Phase: "Phase 2",Value1: "11", Value2: "13" }
    ]

I can't find any way to achive this.

    function utils(data, field){
        var data = [{ Phase: "Phase 1", Value1: "5", Value2: "4" }, { Phase: "Phase 1", Value1: "3", Value2: "7" }, { Phase: "Phase 1", Value1: "5", Value2: "2" }, { Phase: "Phase 1", Value1: "2", Value2: "1" }, { Phase: "Phase 2", Value1: "1", Value2: "3" }, { Phase: "Phase 2", Value1: "4", Value2: "8" }, { Phase: "Phase 2", Value1: "5", Value2: "1" }, { Phase: "Phase 2", Value1: "1", Value2: "1" }],
        result = Object.values(data.reduce((r, { Phase, ...o }) => {
            if (!r[Phase]) r[Phase] = { Phase };
            Object.entries(o).forEach(([k, v]) => r[Phase][k] = (r[Phase][k] || 0) + +v);
            return r;
        }, {}));

        return result
    }


    utils(data, "Phase")

Here i have written code like this. but, the field should be dynamic here if i change the field Phase to something else is is coming undefined and not coming result incorrect.

I wants to use field what i am sending in function instead of Phase. That has to be dynamic

Please have a look

4
  • Does this answer your question? Javascript merge nested array with same object names? Commented May 11, 2020 at 4:29
  • That output is different. Mine is differentr Commented May 11, 2020 at 4:30
  • its all the same, your just iterating through nested arrays for common values, then joining into new simple array. Some of the answers in that link solve Exactly that with clear step by descriptions on how and why. Step back a little and see it as that, your just changing one little step in a very very very commonly posted question, with literally dozens of helpful posts on SO and elsewhere already Commented May 11, 2020 at 4:34
  • i want help in my code. it is working but want Phase to dynamic Commented May 11, 2020 at 4:38

2 Answers 2

1

this solution might work for you

var arr = [
  { Phase: "Phase 1", Value1: "5", Value2: "4" },
  { Phase: "Phase 1", Value1: "3", Value2: "7" },
  { Phase: "Phase 1", Value1: "5", Value2: "2" },
  { Phase: "Phase 1", Value1: "2", Value2: "1" },
  { Phase: "Phase 2",  Value1: "1", Value2: "3" },
  { Phase: "Phase 2", Value1: "4", Value2: "8" },
  { Phase: "Phase 2", Value1: "5", Value2: "1" },
  { Phase: "Phase 2", Value1: "1", Value2: "1" }
];

var outPut = arr.reduce((a, b) => {

  //check result array has the phase
  let c = a.find(e => e.Phase == b.Phase);
  
  //if result array has the phase, increase properties values
  if (c) {
    c.Value1 = String(parseInt(c.Value1) + parseInt(b.Value1));
    c.Value2 = String(parseInt(c.Value2) + parseInt(b.Value2));
  } 
  //if not add current object into result array
  else
    a.push(b);

  return a;

}, []);

console.log(outPut);

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

Comments

0

You can use array#reduce and accumulate based on Phase.

const data = [ { Phase: "Phase 1",Value1: "5", Value2: "4" }, { Phase: "Phase 1",Value1: "3", Value2: "7" }, { Phase: "Phase 1",Value1: "5", Value2: "2" }, { Phase: "Phase 1",Value1: "2", Value2: "1" }, { Phase: "Phase 2",Value1: "1", Value2: "3" }, { Phase: "Phase 2",Value1: "4", Value2: "8" }, { Phase: "Phase 2",Value1: "5", Value2: "1" }, { Phase: "Phase 2",Value1: "1", Value2: "1" } ],
      result = Object.values(data.reduce((r, o) => {
        r[o.Phase] = r[o.Phase] || {Phase: o.Phase, Value1: 0, Value2: 0};
        r[o.Phase].Value1 += +o.Value1;
        r[o.Phase].Value2 += +o.Value2;
        return r;
      },{}));
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.