Evaluating Javascript Arrays

I have an array containing an array of arrays, if that makes sense. eg,

[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]

I want to see if an array exists with an array, so if [1, 2, 3] is duplicated at all. I tried using the .indexOf method, but it finds a duplicate. I also tried Extjs to loop through the array manually and to evaluate each internal array, here is how I did it:

var arrayToSearch = [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]];        
var newArray = [1, 2, 3];
Ext.each(arrayToSearch, function(entry, index){
                    console.log(newArray, entry);
                    if(newArray == entry){
                        console.log(index);
                    };
                });

It also does not detect duplicate. console.log will output [1, 2, 3] and [1, 2, 3], but will not recognize them as equal. I also tried the evaluator ===, but obviously since == doesn't work === wont work. I am at the end, any suggestions.

+3
source share
7 answers

== === , . , .

, - join(','), . :

function arraysAreEqual (a, b) {
  if (a.length != b.length) {
    return false;
  }

  for (var i=0; i<a.length; i++) {
    if (a[i] != b[i]) {
      return false;
    }
  }

  return true;
}

function containsArray (arrays, target) {
  for (var i=0; i<arrays.length; i++) {
    if (arraysAreEqual(arrays[i], target)) {
      return true;
    }
  }

  return false;
}

var arraysToSearch = [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]];
var newArray = [1, 2, 3];
containsArray(arraysToSearch, newArray);
+10

, - .

if(newArray.join('/') == entry.join('/')) ...

, :

if(newArray.length == entry.length && newArray.join('/') == entry.join('/'))

, , . , :

var a = [1, '2/3'];
var b = [1, 2, 3];

, - , - ...

+3

, , , - . .

, Array#join , :

var a = [1, 2, 3];
var b = [1, 2, 3];
alert(a.join(",") == b.join(",")); // Alerts "true"

... a.join(",") "1,2,3", b.join(","). , , , , , . () , JavaScript join , , , , , .

+2

, 2 ===.

+1

:

javascript:
function sameRAs(newArray,entry){
     return newArray.length == entry.length &&
                                  newArray.join('/') == entry.join('/')};
     alert( sameRAs(  [1,'2/3',4],  [1,2,'3/4']  )  ?  'same' : 'different' );
     alert( sameRAs(    [null],         [,]      )  ?  'same' : 'different' );

, [null] [,] , :

 javascript:  alert( [null][0] );  alert( [,][0] );

null undefined .


! == !

 javascript:
      ra=[1,2,3]; ar=[4]; r=[]; composite=[ar,r,ra,ar,ra];
      for(i in composite)
         if(composite[i]==ra)
            alert( JSON.stringify(composite) +' at ['+ i +']'+' == ['+ ra +']')

:

[[4],[],[1,2,3],[4],[1,2,3]] at [2] == [1,2,3]

[[4],[],[1,2,3],[4],[1,2,3]] at [4] == [1,2,3]

.toSource() (& Mozilla), JSON.

 javascript:
      ra=[1,2,3]; ar=[1,2,3];
      alert([  ra==ar,   JSON.stringify(ra)==JSON.stringify(ar)  ]);

false,true.


: . , . ToSource() .

javascript:
   ra = [0,1,2];   ra[3] = ra;      r2d2 = #2= [0,1,2,#2#];
   alert([ ra==r2d2,  ra.toSource() == r2d2.toSource() ])

false,true ( FireFox).


: , . , , . . .

.toSource() , FF . , .

+1

. (, , , , )

Note. If you pass on things like {document}, you find yourself in a world of resentment, but simple stuff works.

function compare(a,b){
  if(a instanceof Array && b instanceof Array){
    if(a.length != b.length){
      return false;
    }
    for(var i=0,l=a.length;i<l;i++){
      if(!compare(a[i], b[i])){
      return false;
      }
    }
    return true;
  } else if(typeof(a) == 'object' && typeof(b) == 'object'){
    var keys = {};
    for(var i in a){
      keys[i] = true;
      if(!compare(a[i], b[i])){
      return false;
      }
    }
    //what if b contains a key not in a?
    for(var i in b){
      if(!keys[i]){
      return false;
      }
    }
    return true;
  } else {
    return (a == b);
  }
}

var someDate = new Date();
var someFunc = function(){alert('cheese');};

var foo = {};
foo['a'] = 'asdf';
foo['b'] = 'qwer';
foo['c'] = 'zxcv';
foo['d'] = ['a','b','c','d','e'];
foo['e'] = someDate;
foo['f'] = 34;
foo['g'] = someFunc

var bar = {};
bar['a'] = 'asdf';
bar['b'] = 'qwer';
bar['c'] = 'zx' + 'cv';
bar['d'] = ['a','b','c','d','e'];
bar['e'] = someDate;
bar['f'] = 34;
bar['g'] = someFunc

if(compare(foo, bar)){
  alert('same!');
} else {
  alert('diff!');
}
0
source

One possible alternative to explore would be to use Array.toSource ():

>>> [3, 2, 1].toSource() == [3, 2, 1].toSource()
true
0
source

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


All Articles