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) {
        //...
            }
        }
4
  • 2
    use indexOf or includes Commented Aug 6, 2017 at 10:37
  • 1
    Possible duplicate of How do I check if an array includes an object in JavaScript? Commented Aug 6, 2017 at 10:45
  • @nisarg ii dont see an object :/ Commented Aug 6, 2017 at 10:48
  • Originally I was going to flag with this, but that is already marked a duplicate. Commented Aug 6, 2017 at 10:50

4 Answers 4

9
 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");
 }
}
Sign up to request clarification or add additional context in comments.

Comments

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);

5 Comments

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 arrow functions are better supported than Array.includes. Array.includes is not supported in Edge, while arrow functions are.
Yes, arrow functions are better supported than .inlcudes(). However, Array.prototype.includes() was supported in Edge as of version 14. Thus, the primary thing one gets by not using .includes() is IE support, for which you must also not use arrow functions.
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.
2024 here, your discussion amuses me.
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);

Comments

0
ids.filter(id => items.s.includes(id))

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

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.