Compare two items of the same array in JavaScript

316 views Asked by At

I'm using react for this code. For a time now I been searching for how to do this, I have a array of arrays, and I need to compare if the beginning of the array is equal to beginning of the other array, then get all the next values of both arrays e put them in arrays that will populated a other array.

const chartValues = () => {
    dataValues.forEach((item) => {
      dataValues.forEach((item2) => {
        if ((JSON.stringify(item.slice(0, group.length))===JSON.stringify(item2.slice(0, group.length)))) {
          item.slice(group.length,group.length+select.length+1).forEach((item4, i)=>{
            setArrayData([...arrayData, ['{' + item.slice(0, group.length) +','+ select[i-1] + '}', item[group.length +i-1], item2[group.length + i-1]]])
          })
        } 
      })
    })
  }

I think it only compares with itself, never get to compare with item2.

dataValues = [
   ['g1','g2',1,2],
   ['g1','g2',3,4],
]

group = ['G1','G2']

select = ['F1','F2']

The result that i get from that is

arrayData= 
[ [ '{g1,g2,F1}', 1,1 ] ];

I want

arrayData=
[ [ '{g1,g2,F1}', 1, 3] , ['{g1,g2,F2}', 2,4] ];
0

There are 0 answers