0

Here is a first sample of code that work as intended: on the rest of the code this is used as a filter and will match 2 items from myids, those 2 where objectId match tWOsQhsP2Z and sStYrIU6lJ:

return  myids.objectId === "tWOsQhsP2Z" || myids.objectId === "sStYrIU6lJ";

Because I need to pass arbitrary number of ids from an array, i'm trying to refactor code like so:

return  myids.objectId === ("tWOsQhsP2Z" || "sStYrIU6lJ");

Problem with this new code is that filter that use return value will return only one item, the one with objectId that is tWOsQhsP2Z.

Do you know a way how I could refactor this second code so I keep single code "myids.objectId" but return match for ALL objectIds values ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Robert Brax
  • 6,508
  • 12
  • 40
  • 69
  • 1
    You could use some form of "include?" in JS, e.g., put the options in an array, and see if the objectId is contained in that array. You could do this manually or use any of several libraries that provide this functionality for you, like lo-dash, underscore, jQuery, etc. – Dave Newton Jan 23 '14 at 17:28
  • 1
    `(["tWOsQhsP2Z", "sStYrIU6lJ"].indexOf(myids.objectId) != -1)` – Niccolò Campolungo Jan 23 '14 at 17:30
  • If the IDs are in an Array, I'm not understanding how `=== ("tWOsQhsP2Z" || "sStYrIU6lJ")` relates since that shows the values hardcoded. Are you saying you want to search the array? And if so, have you looked for the answer, because this is a *very* common task. – cookie monster Jan 23 '14 at 17:30
  • Could be a duplicate of this: [short hand for chaining logical operators in javascript?](http://stackoverflow.com/questions/2932131/short-hand-for-chaining-logical-operators-in-javascript) ...or for this: [Best way to find an item in a JavaScript array?](http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array) Both answer were easy to find with just a wee bit of effort. – cookie monster Jan 23 '14 at 17:38

3 Answers3

1

You can use a switch:

switch (myids.objectId) {
  case "tWOsQhsP2Z":
  case "sStYrIU6lJ":
    return true;
}
return false;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • This wouldn't be practical with an Array of IDs of arbitrary length. I *think* that's what OP is asking about. – cookie monster Jan 23 '14 at 17:32
  • @cookiemonster: Well, perhaps the OP intended to ask about that, but didn't come close to actually doing that... – Guffa Jan 23 '14 at 17:35
  • Yeah, well *"Because I need to pass arbitrary number of ids from an array"* sounds fairly clear, but OP's "attempt" at a solution doesn't make sense. – cookie monster Jan 23 '14 at 17:36
1

Sounds like you need something like underscore.js contains() method, would make things a lot simpler all round.

e.g.

return _.contains(arrayOfIds, myids.objectId);
Jon Miles
  • 9,605
  • 11
  • 46
  • 66
  • This is exactly what I was trying to do, and that works, thanks, i'll see if I can do this without underscore – Robert Brax Jan 23 '14 at 17:43
  • If you're working with an array and don't want to use underscore then you could just use indexOf e.g. `return (arrayOfIds.indexOf(myids.objectId) !== -1);` – Jon Miles Jan 23 '14 at 17:45
0

If you have an array of values to search, and the list is long and/or you're searching frequently, you can convert the list to an object and then do a property lookup. It's much more efficient that searching through an array.

For a simple constant case, your example would look like:

return  myids.objectId in {"tWOsQhsP2Z": 1, "sStYrIU6lJ": 1};

If you start with an array that's server-generated or dynamic:

var knownIds = [ ... ];

then you can convert that to a map:

var idMap = knownIds.reduce(function(m, v) {
  m[v] = 1;
  return m;
}, {});

Now your lookup would be simply:

return myids.objectId in idMap;
Pointy
  • 405,095
  • 59
  • 585
  • 614