Validating an array in a multidimensional array (pref-jQuery)

I have a multidimensional array, and I'm trying to check if it contains another array. I am using jQuery.inArray function at the moment (I tried Array.prototype but kept getting errors, never used this before).

I am trying to ensure that my parent array does not add the same child array twice

if (jQuery.inArray (new Array (step [0], step [1], r2), unavailArray) == - 1) {
    alert (jQuery.inArray (new Array (step [0], step [1], r2), unavailArray));

    unavailArray.push (new Array (step [0], step [1], r2));
  }

I also tried

jQuery.inArray ("[step [0], step [1], r2]", unavailArray) == - 1

and

jQuery.inArray ([step [0], step [1], r2], unavailArray) == - 1

they all return -1, and when I look at the array, I have

[[630,690,09],[3180,3220,2],[3180,3220,2]]

- .

+3
1

, , new Array() , , , , , , .

, , new Array([step[0],step[1],r2]) new Array()

var blah = new Array(step[0],step[1],r2);

// this will add the array to unavailArray
if(jQuery.inArray(blah,unavailArray)==-1){
    alert(jQuery.inArray(blah,unavailArray));

    unavailArray.push(blah);
  }else{
    alert('found so was not added');
}

// Try again and it wont, instead firing off the alert message
if(jQuery.inArray(blah,unavailArray)==-1){
    alert(jQuery.inArray(blah,unavailArray));

    unavailArray.push(blah);
  }else{
    alert('found so was not added');
}

JSFiddle, , :

+3

Source: https://habr.com/ru/post/1781311/


All Articles