Search code examples
typescriptmultidimensional-array

Typescript - Insert element into mutidimensional array if not exists


In one of my angular application I have interface

export interface box {
    row : number;
    col : number;
    value : string;
    selected : boolean; 
};

And in component.ts ,

obj : box[][] = []
allobj : any = []

In further I do get obj as,

this.obj = 
    [
        [{"row":0,"col":0,"value":"00"},{"row":0,"col":1,"value":"01"},{"row":0,"col":2,"value":"02"}],
        [{"row":1,"col":0,"value":"10"},{"row":1,"col":1,"value":"11"},{"row":1,"col":2,"value":"12"}],
        [{"row":2,"col":0,"value":"20"},{"row":2,"col":1,"value":"21"},{"row":2,"col":2,"value":"22"}]
    ]

and will be pushing this object to another array.

this.allobj.push(this.obj)

In next iteration, get obj as

this.obj = 
        [
            [{"row":0,"col":0,"value":"99"},{"row":0,"col":1,"value":"01"},{"row":0,"col":2,"value":"02"}],
            [{"row":1,"col":0,"value":"10"},{"row":1,"col":1,"value":"11"},{"row":1,"col":2,"value":"12"}],
            [{"row":2,"col":0,"value":"20"},{"row":2,"col":1,"value":"21"},{"row":2,"col":2,"value":"22"}]
        ]

And this will be pushed to allobj as one of first value is different (00 & 99). In short , i want to insert obj array into allobj array if not exists. means any of the "value" in this.obj is not already present in allobj. I tried below solution but that is also not working.

let temp=JSON.parse(JSON.stringify(this.obj))
let arr_str = this.allobj.map(JSON.stringify)
!arr_str.includes(JSON.stringify(temp)) && this.allobj.push(temp) 
this.allobj.push(temp) 

need some suggestion.

Playground Demo 2


Solution

  • You can use this function to set the values of both arrays:

    let obj : Box[][] = [];
    let allobj: Box[][][] = [];
    
    function setValues(val: Box[][]) {
      obj = val;
      const allowedVals = allobj.flat().flat();
      const notFound = val.flat().find(b => 
        allowedVals.find(
          a => a.value === b.value &&
            a.row === b.row &&
            a.col === b.col
        ) === undefined
      );
      if (notFound) {
        allobj.push(val);
      }
    }
    

    The function looks for any value in any Box object.

    1st call:

    setValues([
      [
        {"row":0,"col":0,"value":"00"},
        {"row":0,"col":1,"value":"01"},
        {"row":0,"col":2,"value":"02"}
      ],
      [
        {"row":1,"col":0,"value":"10"},
        {"row":1,"col":1,"value":"11"},
        {"row":1,"col":2,"value":"12"}
      ],
      [
        {"row":2,"col":0,"value":"20"},
        {"row":2,"col":1,"value":"21"},
        {"row":2,"col":2,"value":"22"}
      ]
    ]);
    
    console.log(obj.flat().length,allobj.length); // 9, 1
    

    2nd call:

    setValues([
      [
        {"row":0,"col":0,"value":"99"}, // <-- only difference
        {"row":0,"col":1,"value":"01"},
        {"row":0,"col":2,"value":"02"}
      ],
      [
        {"row":1,"col":0,"value":"10"},
        {"row":1,"col":1,"value":"11"},
        {"row":1,"col":2,"value":"12"}
      ],
      [
        {"row":2,"col":0,"value":"20"},
        {"row":2,"col":1,"value":"21"},
        {"row":2,"col":2,"value":"22"}
      ]
    ]);
    
    console.log(obj.flat().length, allobj.length); // 9, 2
    

    Playground demo

    Playground demo 2