0

I have an object like:

item: {
    b: null
    c: "asd"
    i: 10
    q: 10
    s: Array [237,241]}

Also I have an array of ids:

var ids = [237, 238, 239, 240, 242, 243...]

I dont know how to check if above ids exist in s, and then save those items to new array or object

        for (var key in items) {
            for (var i in items[key].s) {
        //...
            }
        }
wezeweze
  • 131
  • 1
  • 3
  • 13

4 Answers4

8
 if(items.s.some(el => ids.includes(el))) alert("wohoo");

Simply check if some of the items ids is included in the ids array. Or using a for loop:

for(var i = 0; i < items.s.length; i++){
 if( ids.includes( items.s[i] )){
  alert("wohoo");
 }
}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
2

You can use Array.filter and Array.indexOf. I assume that you are not using any code transpiler, si I recommend using indexOf instead of includes as it has better browser support.

var foundIds = item.s.filter(x => ids.indexOf(x) !== -1);
// foundIds now contains the list of IDs that were matched in both `ids` and `item.s`

var item = {
    b: null,
    c: "asd",
    i: 10,
    q: 10,
    s: [237,241]
}
var ids = [237, 238, 239, 240, 242, 243];

var foundIds = item.s.filter(x => ids.indexOf(x) !== -1);
console.log(foundIds);
XCS
  • 27,244
  • 26
  • 101
  • 151
  • 2
    It's ironic for you to not use `.includes()` due to it not being well supported, but then use an arrow function. The main thing that you would be trying to gain by not using `.includes()` would be IE support. IE doesn't support arrow functions either. – Makyen Aug 10 '17 at 23:49
  • @Makyen arrow functions are better supported than Array.includes. Array.includes is not supported in Edge, while arrow functions are. – XCS Aug 11 '17 at 17:20
  • Yes, arrow functions are better supported than `.inlcudes()`. However, [`Array.prototype.includes()` was supported in Edge as of version 14](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Browser_compatibility). Thus, the primary thing one gets by not using `.includes()` is IE support, for which you must also not use [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibility). – Makyen Aug 11 '17 at 18:38
  • What I meant, is that `arrow functions` are from ES2015 and `.includes()` are from ES2016. I don't know the exact browser support, but I guess on mobile the difference is pretty large. Also, you are right, using arrow functions while mentioning the browser support issue was a bit ironic. – XCS Aug 11 '17 at 18:45
1

var item = {
  b: null,
  c: "asd",
  i: 10,
  q: 10,
  s: [237,241]
}
var ids = [237, 238, 239, 240, 242, 243];
// way number 1
for(var i = 0; i < item.s.length; i++){
  if( ~ids.indexOf(item.s[i])){
    console.log(item.s[i]);
  }
}
//way number 2
var myArr = item.s.filter(x => ~ids.indexOf(x));
console.log(myArr);
qiAlex
  • 4,290
  • 2
  • 19
  • 35
0
ids.filter(id => items.s.includes(id))

"Filter" the list of ids to those which items.s "includes".