how to check in array value is it string javascript code example

Example 1: Checking whether a value exists in an array javascript

const fruits = ['apple', 'banana', 'mango', 'guava'];

function checkAvailability(arr, val) {
  return arr.some(arrVal => val === arrVal);
}

checkAvailability(fruits, 'kela');   // false
checkAvailability(fruits, 'banana'); // true

Example 2: check if array does not contain string js

function checkInput(input, words) {
 return words.some(word => input.toLowerCase().includes(word.toLowerCase()));
}

console.log(checkInput('"Definitely," he said in a matter-of-fact tone.', 
["matter", "definitely"]));

Example 3: check if array does not contain string js

function checkInput(input, words) {
 return words.some(word => new RegExp(word, "i").test(input));
}

console.log(checkInput('"Definitely," he said in a matter-of-fact tone.', 
["matter", "definitely"]));

Example 4: check if an element is already in an array

private boolean ZitAlInArray(int value, List<Integer> list) {
  return array.indexOf(value) > -1;
}

private boolean zitAlInArray(int[] array, int index) {
		for (int i = 0; i < index; i++) {
			if (array[i] == array[index]) {
				return true;
			}
		} 
		return false;
	}

Tags:

Php Example