Javascript 2d array indexOf

前端 未结 9 1595
既然无缘
既然无缘 2020-11-28 12:03

I have a 2d array like this:

var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];

Each index stores an inner array containing the coordinates of some

9条回答
  •  -上瘾入骨i
    2020-11-28 12:23

    Very simple without indexOf...

    var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];
    const isDup = (x,y) => {
       arr.find(it => JSON.stringify(it) == JSON.stringify([x,y])) == undefined ? arr.push([x,y]) : null
    }
    
    console.log(isDup(2,3)) /* Does not add */
    console.log(isDup(1,2)) /*Does add*/
    console.log(arr) /*Confirmation*/

提交回复
热议问题