javascript - Issue inside for..in loop -


i'm creating function checks whether 2 arrays identical or not , i'm stuck @ checking whether 2 objects (that inside array) identical.

to explain code little bit, have variable named eq that's returned when function on , should contain either true each element of first array exists in second array or undefined if element doesn't.

also, use recursive iife check if object has sub-objects , sub-objects identical. check whether array element object literal use el.constructor === object.

without being 100% certain, believe i've done wrong inside for..in loops.

code:

function equals(a, b) {    return (a === b && !== null) || (a.length === b.length && (function(a, b) {      var eq = [];        a.foreach(function(el1, index) {        b.foreach(function(el2) {          if (el1 === el2) eq[index] = true;          else if (el1.constructor === el2.constructor && el1.constructor === object) {            /* -> object of first array             * b -> object of second array             * c -> eq[index] eq[index][i] provided how deep goes */            (function rec(a, b, c) {              c = [];              var = 0;              (var prop in a) {                (var attr in b) {                  if (prop === attr && a[prop] === b[attr]) c[i] = true;                  else if (prop === attr && a[prop].constructor === b[attr].constructor                    && a[prop].constructor === object) {                      rec(a[prop], b[attr], eq[index][i]);                    }                }                i++;              }            })(el1, el2, eq[index]);          }        });      });      return /*!~eq.indexof(undefined);*/ eq;    })(a, b));  }    /* use */  var = [1, {a: "a", b: "b" }, 4, 6],      b = [{a: "a", b: "b"}, 1, 7, 6];    equals(a, b);


example 1: (works fine simple arrays)

var    = [1, 3, 4, 6],    b = [3, 1, 7, 6];  equals(a, b); // returns: [true, true, undefined, true] 

example 2: (doesn't work objects)

var    = [1, {a: "a", b: "b"}, 4, 6],    b = [{a: "a", b: "b"}, 1, 7, 6];  equals(a, b); /* returns: [true, undefined, undefined, true]                  should return: [true, [true, true], undefined, true] */ 

any appreciated.

i think problem lies in eq[index] being passed comparator function. doesn't pass reference, undefined value. setting c = [] inside function overriding undefined new array. there no connection eq whatsoever. restructure code either create array in eq[index] outside rec , pass in (c). or make function rec return value.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -