-1

sample data

 array_data = [
  [6326586, 0.4126],
  [6326586, 0.48],
  [6326586, 0.45],
  [6326586, 0.35],
  [6326586, 0.2685],
  [6326614, 0.4008],
  [6326614, 0.1],
  [6326614, 0.074],
  [6327407, 0.066],
  [6327408, 0.1],
  [6344999, 0.1572],
  [6344999, 0.003],
  [6394500, 0.2112]
]

I have a nested array if the first index value is the same as the other value then sum of these values as show below result

I need an expected array

new_array_data = [
  [6326586, 1.9611],
  [6326614,0.5748],
  [6327407, 0.066],
  [6327408, 0.1],
  [6344999, 0.1602],
  [6394500, 0.2112],
]

Thanks

0

2 Answers 2

2

Using Map and Array#Reduce in Vanilla JS

const 
  arr = [[6326586,.4126],[6326586,.48],[6326586,.45],[6326586,.35],[6326586,.2685],[6326614,.4008],[6326614,.1],[6326614,.074],[6327407,.066],[6327408,.1],[6344999,.1572],[6344999,.003],[6394500,.2112]], 

  res = Array.from(arr.reduce((m, [k, v]) => m.set(k, (m.get(k) || 0) + v), new Map()))

console.log(res)

Using Object and Array#Reduce in Vanilla JS

** Go through the comments before using this

const 
  arr = [[6326586,.4126],[6326586,.48],[6326586,.45],[6326586,.35],[6326586,.2685],[6326614,.4008],[6326614,.1],[6326614,.074],[6327407,.066],[6327408,.1],[6344999,.1572],[6344999,.003],[6394500,.2112]], 
  
  res = Object.values(arr.reduce((o, [k, v]) => (o[k] ??= [k, 0], o[k][1] += v, o), {}));

console.log(res)

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

5 Comments

With object as accumulator, the output has strings and not numbers in 0 index
@adiga Thanks a lot for mentioning this, I completely missed this. I have currently mapped the result, is there any other better option?
@SomShekharMukherjee Its helpful for me thanks
You could change it to how Sebastian Richner's has handled it like o[k] ??= [k, 0], o[k][1] += v and use Object.values() instead of entries. But, object accumulator are a bit unreliable. If you had an entry say, ["constructor",.4126], it will check if constructor exists on the object and it will return Object.prototype.constructor and try to append to that function
@adiga Again an interesting point +1. So, when I am checking if k is in o, for "constructor" it becomes true and then it starts adding to the function. Have I understood it correctly? But I hope OP will not have strings in keys.
2

You can do this in plain JavaScript, you don't need jQuery. You can use Array.reduce as follows:

const array_data = [
  [6326586, 0.4126],
  [6326586, 0.48],
  [6326586, 0.45],
  [6326586, 0.35],
  [6326586, 0.2685],
  [6326614, 0.4008],
  [6326614, 0.1],
  [6326614, 0.074],
  [6327407, 0.066],
  [6327408, 0.1],
  [6344999, 0.1572],
  [6344999, 0.003],
  [6394500, 0.2112]
]

const result = array_data.reduce((acc, [key, value]) => {
  if (acc[key]) {
    acc[key][1] += value;
  } else {
    acc[key] = [key, 0];
  }
  return acc;
}, {});

console.log(Object.values(result));

2 Comments

The output has strings and not numbers in 0 index
You're right, check the updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.