-1
var arr = [{'a':'daina', 'z':12},{'b':'john', 'y':22}, {'c':'alan','z':30}];
var arr2 = [{'c':'john', 'z':62}, {'d':'alana','s':32},  {'e':'mac','t':42}];
var finalArray = [];
for(var key2 in arr) {
  console.log(arr[key2]);
}
function compareKeys(arr,arr2){
  for(var key1 in arr2){
    for(var key in arr){
      if(key1 === key){
        finalArray = arr.splice(key,1);
      }
    }
  }
  for(var key in finalArray) {
    console.log(finalArray[key]);
  }
}
compareKeys(arr,arr2);

I am not getting what is wrong with the code. I want to remove 'c' property from 'arr' after comparing both 'arr' and 'arr2'. The 'c' property is same in both arrays so I want to remove it in my output. The output returned by the function should be {'a':'daina', 'z':12},{'b':'john', 'y':22}

Can anyone help me in comparing the properties of objects?

Pallavi
  • 85
  • 4
  • 15
  • in arra2 c object is {'c':'john', 'z':62} and in arr it is {'c':'alan','z':30} – Tanvi B Jun 03 '16 at 00:11
  • they both are not same – Tanvi B Jun 03 '16 at 00:11
  • yeah but It does not matter what the value are, the properties name should not be same. – Pallavi Jun 03 '16 at 00:22
  • 1
    but the z property also exists in multiple objects in both arrays. What is the condition to keep it. – Redu Jun 03 '16 at 05:02
  • I'm just curious, do you really write code with random indentation, and if so, how do you read it? Is it really that hard to indent properly? Your editor probably has a command to do that for you. –  Jun 03 '16 at 05:34
  • *I want to remove 'c' property from 'arr'*. `arr` does not have a `c` property; it contains an object which includes the `c` property. Do you mean you want to remove all objects which have any property in common with any of the objects in `arr2`? –  Jun 03 '16 at 05:38
  • yes. I want to display only those objets in arr which are not common with arr2 – Pallavi Jun 03 '16 at 16:18
  • Your question is still quite ambiguous. Is this what your want: "I want a list of all those elements of `arr` excluding those for which there is an element of `arr2` with a matching set of keys."? Do we exclude ` {'c':'alan','z':30}` because _both_ 'c' and 'z' are included, or is there something special about 'c'? – Scott Sauyet Jun 04 '16 at 00:41

2 Answers2

0

You're mixing up arrays, objects, keys and values here.

You should read the variables arr and arr2 as

arr[0] = {'a':'daina', 'z':12}
arr[1] = {'b':'john', 'y':22}
arr[2] = {'c':'alan','z':30}]

and

arr2[0] = {'c':'john', 'z':62}
arr2[1] = {'d':'alana','s':32}
arr2[2] = {'e':'mac','t':42}

so the keys of both arrays are 0, 1 and 2, and the values are objects.

In your nested loop

for(var key1 in arr2){
for(var key in arr){
    if(key1 === key){
      ....
}}}

you basically are comparing:

key === key1
  0 === 0    (true)
  0 === 1    (false)
  0 === 2    (false)
etc.

and not the values. You should compare arr[key] === arr2[key1] to compare the values. Sadly you cannot compare two objects just like that.

Take a look at this question. You could either use one of the answers from that to compare the objects, or rethink your data design.

Community
  • 1
  • 1
Michel
  • 4,076
  • 4
  • 34
  • 52
0

Instead of comparing two object like below

 if(arr2[key1] === arr[key])

can you try to compare like below

JSON.stringify(obj1) === JSON.stringify(obj2) 
Tanvi B
  • 1,577
  • 11
  • 14