0

How would you take an array such as this:

arr1 = [[1150-500101-1000005530, 2750.0],  
        [1150-500101-1000005530, 275.0], 
        [1150-500101-1000008371, 825.0],  
        [1150-500101-1000014950, 550.0],  
        [1150-500101-1000019105, 589.0],  
        [1150-500101-1000019111, 589.0],  
        [1150-500101-1000019111, 275.0]]

and merge duplicate values from the first value of each array (arr1[i][0])

in order to get something like this:

reducedArr1 = [[1150-500101-1000005530, 3025.0],
               [1150-500101-1000008371, 825.0],
               [1150-500101-1000014950, 550.0],
               [1150-500101-1000019105, 589.0],
               [1150-500101-1000019111, 864.0]]

These values are changing constantly as they are pulled from a Google Sheet.

I have tried to find similar problems like this but haven't found anything that applies without assigning a key. What I am really looks for is a SUMIF made for two dimensional arrays so I can add this new array to a Google Sheet report.

Thanks

2 Answers 2

0

I believe your goal is as follows.

  • You want to convert the value of arr1 to reducedArr1 using Google Apps Script.

As another approach, how about the following sample script?

Sample script:

// This is your sample input value.
const arr1 = [
  ["1150-500101-1000005530", 2750.0],
  ["1150-500101-1000005530", 275.0],
  ["1150-500101-1000008371", 825.0],
  ["1150-500101-1000014950", 550.0],
  ["1150-500101-1000019105", 589.0],
  ["1150-500101-1000019111", 589.0],
  ["1150-500101-1000019111", 275.0]
];

const reducedArr1 = [...arr1.reduce((m, [a, b]) => m.set(a, m.has(a) ? m.get(a) + b : b), new Map())];
console.log(reducedArr1);

Testing:

When the above script is run, the following result is obtained.

[
  ["1150-500101-1000005530", 3025],
  ["1150-500101-1000008371", 825],
  ["1150-500101-1000014950", 550],
  ["1150-500101-1000019105", 589],
  ["1150-500101-1000019111", 864]
]

References:

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

Comments

0

Summing Duplicate Items in 2d array:

function myfunk() {
  let arr1 = [["1150-500101-1000005530", 2750.0], ["1150-500101-1000005530", 275.0], ["1150-500101-1000008371", 825.0], ["1150-500101-1000014950", 550.0], ["1150-500101-1000019105", 589.0], ["1150-500101-1000019111", 589.0], ["1150-500101-1000019111", 275.0]];
  var obj = { pA: [] };
  arr1.forEach((r,i) =>  {
    let p = r[0];
    if(!obj.hasOwnProperty(p)) {
      obj[p] = {id:r[0],value:r[1]};
      obj.pA.push(p);
    } else {
      obj[p].value += r[1];
    }
  })
  let arr = obj.pA.map(p => [obj[p].id,obj[p].value]);
  Logger.log(JSON.stringify(arr));
}

Execution log
10:45:26 AM Notice  Execution started
10:45:27 AM Info    [["1150-500101-1000005530",3025],["1150-500101-1000008371",825],["1150-500101-1000014950",550],["1150-500101-1000019105",589],["1150-500101-1000019111",864]]
10:45:28 AM Notice  Execution completed

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.