How to compare indexes of two objects and return what is different?

Asked

Viewed 528 times

3

I would like to compare these two objects and return the index that is different.

In this case it is to return the 4 of the second object (will always be different in the second).

var arr = {
   1 : 'Corsa',
   2 : 'Ka',
   3 : 'Pálio'
};

var foo = {
   1 : 'Corsa',
   2 : 'Ka',
   3 : 'Pálio',
   4 : 'Jeep'
};

How do I do this in Javascript or using jQuery ?

  • And if there’s more, if there’s 5 you want both (4 and 5) or only 5?

  • It will always be 1 different.

  • There’s only one, okay I get it

  • And the order will always be the same, equal values will always have the same index or if you have different order also return?

  • In fact it will come in alphabetical order.

  • I removed the answer, because I can’t fix it now. But feel free to post something cooler. In principle the logic is the same, iterate one and see if you have it in the other, and accumulate the results when you don’t find it. @Moshmage grateful for the warning. Anderson, the doubt you put in the answer is pertinent.

  • @Bacco I understood its logic. Even why array is not so different from treating with respect to object.

Show 2 more comments

3 answers

3


for objects, you can do as follows.:

Object.defineProperty(Object.prototype, "except", {
  enumerable: false,
  value: function (outro)  {
    var keysB = Object.keys(outro);
    return Object.keys(this).reduce(function (resultado, key) {
      if (keysB.indexOf(key) == -1)
        resultado[key] = this[key];
      return resultado;
    }.bind(this), {});
  }
});

var foo = {
  1 : 'Corsa',
  2 : 'Ka',
  3 : 'Pálio'
};

var bar = {
  1 : 'Corsa',
  2 : 'Ka',
  3 : 'Pálio',
  4 : 'Jeep'
};

console.log(bar.except(foo));

  • 2

    +1, I think it’s legible.

2

Another way is to convert the object into string and save to an array. In this case, even if the size of the objects is uncertain, in case the first is larger than the second, it will work.

var arr = {
  1: 'Corsa',
  2: 'Ka',
  3: 'Pálio'
};

var foo = {
  1: 'Corsa',
  2: 'Ka',
  3: 'Pálio',
  4: 'Jeep'  
};

var obj1 = JSON.stringify(arr).replace(/{|}/g, '').split(",");
var obj2 = JSON.stringify(foo).replace(/{|}/g, '').split(",");
var maior = Math.max(obj1.length, obj2.length);

for (var i = 0; i < maior; i++) {
  if (obj1.length > obj2.length) {
    if (obj1[i] != obj2[i]) {
      console.log("índice diferente: " + obj1[i].split(":")[0] + " entre os objs");
      break;
    }
  } else {
    if (obj2[i] != obj1[i]) {
      console.log("índice diferente: " + obj2[i].split(":")[0] + " entre os objs");
      break;
    }
  }
}

1

You need to know which Object has the most keys, and then iterate until you find a key the other doesn’t have. You can do it like this:

function comparar(a, b) {
    var axb = Object.keys(a).length > Object.keys(b);
    var mairObjeto = axb ? a : b;
    var menorObjeto = axb ? b : a;

    for (var i in mairObjeto) {
        if (!menorObjeto.hasOwnProperty(i)) return i
    }
}

An example would be like this:

var foo = {
    1: 'Corsa',
    2: 'Ka',
    3: 'Pálio'
};

var bar = {
    1: 'Corsa',
    2: 'Ka',
    3: 'Pálio',
    4: 'Jeep'
};

function comparar(a, b) {
    var axb = Object.keys(a).length > Object.keys(b);
    var mairObjeto = axb ? a : b;
    var menorObjeto = axb ? b : a;

    for (var i in mairObjeto) {
        if (!menorObjeto.hasOwnProperty(i)) return i
    }
}

console.log(comparar(foo, bar));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.