-1

I have below array ,

["aa","bb"] 

I have below object,

{"aa":1,"bb":2,"cc":3}

and I need result below,

{"cc":3}

I need to compare array with object keys, using looping it is possible,but it take more time for large amount data, is there any other fastest way to get my result without using loop, Anyhelp Appreciated...

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65
Thiyagu
  • 746
  • 3
  • 11
  • 29

6 Answers6

2

To avoid quadratic time complexity you could use a Set and pass that as the this argument to the filter. Then recompose an object from those filtered keys with Object.assign:

var a = ["aa","bb"];
var b = {"aa":1,"bb":2,"cc":3};

var difference = Object.assign(...Object.keys(b).filter(function(e){
  return !this.has(e);
}, new Set(a)).map(key => ({ [key]: b[key] }) ));

console.log(difference);
trincot
  • 317,000
  • 35
  • 244
  • 286
1

If you are OK with mutating the object, then you can use delete to remove the unwanted properties.

const props = ["aa","bb"];
const obj = {"aa": 1,"bb": 2,"cc": 3};

props.forEach(prop => delete obj[prop]);

console.log(obj);

Or just loop over the array of property names with for...of.

const props = ["aa","bb"];
const obj = {"aa": 1,"bb": 2,"cc": 3};

for (prop of props) delete obj[prop];

console.log(obj);

If you don't want to mutate the object, but instead create a new one without he unwanted properties, then you can do a shallow clone first with

newObj = {...obj};
  • @Nina `props` was supposed to be the array, so `for (prop of props)` takes its values one by one. I've clarified that. –  Jul 21 '17 at 00:57
0

Something like this maybe?

var a = ["aa","bb"];
var b = {"aa":1,"bb":2,"cc":3};

var result = {};

Object.keys(b).filter(function(e){
  return a.indexOf(e) < 0;
}).forEach(function(e){
  result[e] = b[e];
});


console.log(result);
Ivan Sivak
  • 7,178
  • 3
  • 36
  • 42
  • You can alternatively use `array.reduce` as OP wants an object with missing keys and their values – Rajesh Jul 20 '17 at 13:11
  • This is a bad code. OP has clearly said that it is a BIG object. So looping twice is a bad idea. try `var result = Object.keys(obj).reduce( (p,c)=> (!keys.includes(c) && p[c] = obj[c], p) ,{})` – Rajesh Jul 20 '17 at 13:17
  • 1
    @Rajesh, looping a second time with `forEach` is not an issue here. Nesting a call to `includes` (or `indexOf`) will have a much bigger time impact (for larger arrays) than that. It makes the time complexity *O(n²)* instead of *O(n)*. The `forEach` does not increase the time complexity above *O(n)*. – trincot Jul 20 '17 at 13:28
  • @trincot even Ivan is using `.indexOf`. All I did was used `.reduce` instead of `.filter + .forEach`. My point was, adding an `if` can remove the need fpr second loop – Rajesh Jul 20 '17 at 13:42
  • 2
    Sure, you can remove the need for a second loop, but to call Ivan's code "bad code" just because of the second loop is really not warranted. – trincot Jul 20 '17 at 13:45
0

Loop through the object and check to see if the property doesn't exist in the array:

var a = ["aa","bb"];
var o = {"aa":1,"bb":2,"cc":3};

// Loop through object
for(var prop in o){
  // If property doesn't exist in array...
  if(a.indexOf(prop) === -1){
    console.log(prop + ":" + o[prop]);
  }  
}
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

you can loop through the object and compare the object keys with the array items and return the missing keys as an object.

var a = ["aa","bb"] ;
var o = {"aa":1,"bb":2,"cc":3,'dd':4};

function get(o){
var p = {};
for(var key in o){
 if(a.indexOf(key) == -1){
     p[key] = o[key];
  }
}
return p;
}
var c = get(o);
console.log(c);
Anurag
  • 643
  • 1
  • 11
  • 28
0

You can use Object.assign(), Object.entries(), Array.prototype.includes() to determine if current key in object is an element of array, return object using computed property or false, which does not result in property being assigned to new object

var arr = ["aa","bb"] 

var obj = {"aa":1,"bb":2,"cc":3}

var res =  Object.assign({}
           , ...Object.entries(obj).map(([key, prop]) =>
               !arr.includes(key) && {[key]:prop})
           );

console.log(res);
guest271314
  • 1
  • 15
  • 104
  • 177