-1

Consider the following array structure. This is a pseudo syntax where Table and Row is just an Array. And Row contains different objects. I use this for an excel sheet like application.

Table [Row, Row, Row, ...]
    0: Row
        0: TextEntry {value: "1", active: false}
        1: TextEntry {value: "3", active: false}
        2: NumberEntry {value: 0, active: false}
    1: Row
        0: TextEntry {value: "text", active: false}
        1: TextEntry {value: "8", active: false}
        2: NumberEntry {value: 0, active: false}
    2: Row
        0: TextEntry {value: "1", active: false}
        1: TextEntry {value: "3", active: false}
        2: NumberEntry {value: 0, active: false}
    ...

I want to determine if there is a Row inside inside Table, which identical to another Row by comparing the value attributes. In this example the array on index 0 whould be identical to the one on index 2.

How could I achieve that in js?

ver.i
  • 526
  • 1
  • 5
  • 13

1 Answers1

2

I assume that the combination of some and every function of the Array prototype should be enough. Version with some returns a boolean result. Version with filter returns the rows themself. Unless what you wanted to achieve is checking whether the table contains duplicated rows? I added an example of that below.

const table = [
  [
      {value: "1", active: false},
      {value: "3", active: false},
      {value: 0, active: false}
  ],
  [
      {value: "text", active: false},
      {value: "8", active: false},
      {value: 0, active: false}
  ],
  [
      {value: "1", active: false},
      {value: "3", active: false},
      {value: 0, active: false}
  ]
];

const rowToFind=[
  {value: "1", active: false},
  {value: "3", active: false},
  {value: 0, active: false}
];


let foundRows=table.filter(m => m.every((el, indx, arr)=>rowToFind[indx].value==el.value));
let foundRowsBoolean=table.some(m => m.every((el, indx, arr)=>rowToFind[indx].value==el.value));

console.log(foundRows);
console.log(foundRowsBoolean);

//Check duplicated rows
let foundDuplicates=table.some((el, indx)=> table.some((el2, indx2)=> indx!=indx2 && el.every((e, indx)=>e.value==el2[indx].value)));
console.log(`Contains duplicates: ${foundDuplicates}`);
Piotr G
  • 133
  • 8