2

I have an array with keys like ["1", "5", "9"] And I have an object with the same keys, something like this: selections: { "5": {}, "12": {} }

What is the easiest way to get a boolean value out of it. It should return true if any of the keys in the array is present in my object. I am using angular and lodash, is there any smart solution for this or do I need to do a loop for it? And if I do a loop, what is the most efficient way?

3
  • 2
    What did you try yourself? Commented Oct 25, 2015 at 17:34
  • I tried _.every(["1", "2", "5"], function (id) { if (!_.includes(_.pluck(selections, "id"), id)) { return true; } }); Commented Oct 25, 2015 at 17:40
  • See @Pavel Komiagin answer... Commented Oct 25, 2015 at 17:41

5 Answers 5

3

Did you try to use hasOwnProperty()?

function check() {
  var selections = { "5": {}, "12": {} };
  return ["1", "5", "9"].filter(function(value) {
    return selections.hasOwnProperty(value);
  }).length > 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Nice and tidy, like it! +1
The problem with this is that it goes through the entire array all of the time. You can stop as soon as you find one match.
what about using array.some rather than filter? No need for the length-check then.
Totally agree, this solution not efficient on large inputs
@Etse the issue with every is that it only returns true if all values in the array pass the predicate - whereas op wants it to return true if any value matches the predicate - so array.some is a better option in this case (and returns immediately if predicate returns true as opposed to going through entire array).
3
var selections = { "5": {}, "12": {} };
var array = ["1", "5", "9"];

array.some(key => selections.hasOwnProperty(key));

1 Comment

Nice use of Arrow functions but it does require ECMAScript 2015 which, for some, may not yet be available on their platform.
1

Just a single line of code:

var array = ["1", "5", "9"],
    obj = { "5": {}, "12": {} },
    any = array.some(function (a) { return a in obj; });
document.write(any);

1 Comment

this is a nice solution, however it only works if all my keys matches the keys in the object. But seems your solution also works with array.some for any key, so that would be it :)
0
var keys = ["1", "5", "9"]; 
var selections = {"5": {}, "12": {}};   

function hasAnyKey(keys, selections) {
  for (var i = 0; i < keys.length; i++) {
    if (selections[keys[i]]) {
      return true;
    }
  }
  return false;
};

hasAnyKey(keys, selections);

This solution will return as soon as there is 1 match, which equates to returning true when there is at least 1 match, which is what you want. In theory, this will work faster than the solutions with Array.prototype.filter for larger inputs.

Comments

0

Array.prototype.some() executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some() immediately returns true. Otherwise, some() returns false. Array.prototype.some() is in ECMAScript 1.5 and should work just about anywhere.

function compareArrayToObject(array, obj) {
    return array.some(function (a) { return a in obj; });
}
var array = ["1", "5", "9"];
var obj = { "5": {}, "12": {} };
var result = compareArrayToObject(array, obj);
document.write(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.