0

Firstly, the .includes method does not work with my framework, so I omit this here. I have checkIn(el,ar) function to check if el in ar like this:

function checkIn(el){
var check = false;
   for (i = 0; i < ar.length; i++){
     if(el === ar[i]){
     check = true;
     break;
   };
  };
 return check;
 };

it works normaly. Then I want to use the .every method to check if every element of a given array is in another given array

var x = [1,2,3];
var y = [1,2,3,4,5];
var check = x.every(checkIn(y))

check needs to be true, but it doesn't work?

Godgog Arsenal
  • 107
  • 1
  • 8
  • 1
    y is an array not an element. – Daksh M. Dec 08 '18 at 03:29
  • Possible duplicate of [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) – Daksh M. Dec 08 '18 at 03:30

3 Answers3

2

.every accepts a callback for which the first parameter is an element of the array being iterated over. If you want to be able to use

x.every(checkIn(y))

then checkIn should return a function that can then be used as the callback:

function checkIn(containingArr) {
  return item => containingArr.includes(item);
};

var x = [1, 2, 3];
var y = [1, 2, 3, 4, 5];
console.log(x.every(checkIn(y))); // every item in x is in y; true
console.log([0].every(checkIn(y))); // 0 is not in y; false

Or, to use something similar to your for loop:

function checkIn(ar) {
  return el => {
    var check = false;
    for (i = 0; i < ar.length; i++) {
      if (el === ar[i]) {
        check = true;
        break;
      };
    };
    return check;
  };
}

var x = [1, 2, 3];
var y = [1, 2, 3, 4, 5];
console.log(x.every(checkIn(y)));
console.log([0].every(checkIn(y)));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

First off, your checkIn(el,ar) does only take el so if you're not getting an error you're accessing a global or out-of-scope variable ar that may hold data that doesn't make sense in that context.

If you simply want to check if an array contains a specific value there is a really simple way to do it:

[1,2,3].includes(3) //true
[1,2,3].includes(6) //false

And to extrapolate to a whole array using .every requires you to pass a lambda or function, not just a function call, like so:

var xs = [1, 2, 3];
var ys = [1, 2, 3, 4, 5];

xs.every(x => ys.includes(x))

where x => ys.includes(x) is shorthand for function(x){return ys.includes(x)}

or using your checkIn(el,ar)

xs.every(x => checkIn(x,ys))
nitowa
  • 1,079
  • 2
  • 9
  • 21
  • I don't want to use th `includes` method here since it does not work with my framework. the satement `el === ar[i] ` is just an example of what I need to do. I just wanna know how the `.every` method work. Sorry for the unclear. – Godgog Arsenal Dec 08 '18 at 03:40
0

Function checkIn looks redundant.

function checkIn(el, ar) {
    let check = false;
    for (let i = 0; i < ar.length; i++) {
        if (el === ar[i]) {
            check = true;
            break;
        };
    };
    return check;
};

Why not use Array.prototype.includes?

BTW, the first parameter of the function every should be a function but you pass the result of checkIn, which type is boolean.

We can do like this.

x.every((e) => y.includes(e))
Victor
  • 366
  • 2
  • 7