0

I have the following array

// Exmaple
[
   ['morning', 'afternoon'],
   ['morning'],
   ['morning', 'afternoon'],
   ['morning', 'afternoon'],
   ['morning']
]

I may have the same one but with afternoon in every array. I need to check if a given value exists in all arrays, for example if I check for 'morning' it should return true, but if I check for 'afternoon' it should return false because in the example array above not all of them have 'afternoon'

1

5 Answers 5

5
  array.every(day => day.includes("morning")) // true
Sign up to request clarification or add additional context in comments.

Comments

3

You can use .every() and .includes() methods:

let data = [
   ['morning', 'afternoon'],
   ['morning'],
   ['morning', 'afternoon'],
   ['morning', 'afternoon'],
   ['morning']
];

let checker = (arr, str) => arr.every(a => a.includes(str));

console.log(checker(data, 'morning'));
console.log(checker(data, 'afternoon'));

Comments

2

You can use Array.every and Array.includes

let arr = [['morning', 'afternoon'],['morning'],['morning', 'afternoon'],['morning', 'afternoon'],['morning']];

console.log(arr.every(v => v.includes('morning'))); // true
console.log(arr.every(v => v.includes('afternoon'))); // false

Comments

1
use .every()

 array.every(d => d.includes("morning")) // true
 array.every(d => d.includes("afternoon")) //false

Comments

0

You can use Array.prototype.every() and coerces to boolean the result of Array.prototype.find() which returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

Code:

const data = [['morning', 'afternoon'],['morning'],['morning', 'afternoon'],['morning','afternoon'],['morning']];
const checker = (arr, str) => arr.every(a => !!a.find(a => str === a));

console.log(checker(data, 'morning'));
console.log(checker(data, 'afternoon'));

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.