-1

i have one list and one DOM Object i need compare and get different value theirs

Example:

lista1 = ["type", "name","visible"] 
lista2 = Object {type: "button", name: "button", class: "buttonBlack", visible: "true", backgroundimage: "null"}

lista2 = Object.keys(list2);

i try:

for(var i = 0; i<lista1.length;i++)

    $.each(lista2,function(key,val){
        if(lista[i] == val){
         list2.remove(val);
        }      
    });
};

I want to get the items that are not equal between them

How can i make this ?

expected return:

["class","backgroundimage"]
user2403131
  • 43
  • 1
  • 9

3 Answers3

2
list1 = ["type", "name","visible"] 
list2 = {type: "button", name: "button", class: "buttonBlack", visible: "true", backgroundimage: "null"}
result = []

$.each(list2, function(k,v){
  if ($.inArray(k, list1) === -1) {
    result.push(k)
  }
})

result
>> ["class", "backgroundimage"]
mgamba
  • 1,189
  • 8
  • 10
1
var list1 = ["type", "name", "class", "visible", "backgroundimage", "disabled", "value"];
var list2 = {type: "button", name: "button", class: "buttonBlack", visible: "true", backgroundimage: "null"};

list2 = Object.keys(list2);

function arr_diff(a1, a2)
{
  var a=[], diff=[];
  for(var i=0;i<a1.length;i++)
    a[a1[i]]=true;
  for(var i=0;i<a2.length;i++)
    if(a[a2[i]]) delete a[a2[i]];
    else a[a2[i]]=true;
  for(var k in a)
    diff.push(k);
  return diff;
}

console.log(arr_diff(list1, list2));

results in

["disabled", "value"] 

** based on
Get array of object's keys
JavaScript array difference

DEMO

Community
  • 1
  • 1
kei
  • 20,157
  • 2
  • 35
  • 62
0
lista1 = ["type", "name","visible"] 
lista2 = Object {type: "button", name: "button", class: "buttonBlack", visible: "true", backgroundimage: "null"}

result = [];

for (var prop in lista2) {
   if (lista2.hasOwnProperty(prop) && lista1.indexOf(prop) < 0) {
       result.push(prop);
   }
}

The array result will contain all the properties of the object that aren't in your array.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171