Javascript - Compare array of objects and sum coincident values -


i have array of objects:

arr=[      {a: 1,b: 0,x: 100},      {a: 0,b: 0,x: 100},      {a: 1,b: 1,x: 100},      {a: 1,b: 0,x: 200},      {a: 1,b: 1,x: 200},      .... ] 

now, need compare "x" values , if coincide, tranfer summed "a" , "b" values in array. example:

arr2=[{a=2,b=1,x=100},{a=2,b=1,x=200}....]

second thing do, count how many objects been joined same "x" value. example in first "arr2" object joined 3 "arr" objects , in second "arr2" object joined 2 "arr" objects , on.

this question doesn't seem make sense. first of all, word "coincident" doesn't have technical definition i'm aware of. can more clear mean?

secondly not clear expected results are. perhaps if start simple example inputs , come expect corresponding output be, use unit testing tool develop code want.

for example, have guess wanting might (in javascript) using libraries chai , mocha:

import { expect } 'chai'  function dowork(input) {   // code goes here }  const tests = [   {     name: 'same x values coalesce',     data: [       {a=1,b=0,x=100},       {a=0,b=0,x=100}     ],     expected: {       100: [1, 0]     }   }  ] describe('work', () => {   tests.foreach(test => {     it(t.name, () => {       const result = dowork(test.data)       expect(result).to.deep.equal(test.expected)     })   }) }) 

this technique may come to answer on own.

try little more precise terms , give example of expecting output.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -