How to get indexOf to check for one or multiple values
One option is to use a regular expression instead:
/Word1|Word2|Word3/.test(string)
Or iterate over an array of words to search for:
var found = false;
var arr = ['Word1', 'Word2', 'Word3']
for (var i = 0; i < arr.length; i++) {
if (string.indexOf(arr[i]) !== -1) {
found = true;
break;
}
}
// use `found` variable
If you want to ensure that the string doesn't contain any of those words with a regex, then continually match characters from the beginning of the string to the end while using negative lookahead for the alternated pattern:
^(?:(?!Word1|Word2|Word3)[\s\S])+$
https://regex101.com/r/FsChvB/1
But that's strange, it'd be a lot easier just to use the same test as above, and invert it.
var stringContainsForbiddenWords = !/Word1|Word2|Word3/.test(string)
indexOf with multiple arguments
The ||
operator returns the left hand side if it is true, otherwise it returns the right hand side. 123 || 124 || 125
just means 123
.
If you want to test if any of multiple values are in an array, then you have to test each one in turn.
array.indexOf(123) == 0 || array.indexOf(124) == 0 || array.indexOf(125) == 0
Since you only care about one specific index the array, you can turn the whole thing on its head:
[123, 124, 125].indexOf(array[0]) > -1
Javascript index of for multiple values
You can use the .filter()
method:
var mainArray = [ /* your array here */ ],
check = ["15", "21"],
result;
result = mainArray.filter(function(val) {
for (var i = 0; i < check.length; i++)
if (val.indexOf(check[i]) === -1)
return false;
return true;
});
console.log(result);
Demo: http://jsfiddle.net/nnnnnn/Dq6YR/
Note that .filter()
is not supported by IE until version 9, but you can fix that.
How to find index of object in array using multiple values with ES5?
ES6+
You can use .findIndex()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
var found = items.findIndex(function(itm) {
return itm.number1 === number1 && itm.number2 === number2;
});
ES5:
Using .filter():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
var foundItems = items.filter(function(itm) {
return itm.number1 === number1 && itm.number2 === number2;
});
if (foundItems && foundItems.length > 0) {
var itemYouWant = foundItems[0];
}
Getting the index--you can have the index value returned as part of the filter method. Check out the docs for more examples.
How to get multiple indexes of array by a value in pure JavaScript (value exact matching)
You can reduce the array to array of indexes, which value is 1
:
const arr = [1, 11, 1, 111, 1111, 11, 1, 1111, 11];
const indexes = arr.reduce((r, n, i) => { n === 1 && r.push(i); return r;}, []);
console.log(indexes);
Getting the index of a value having multiple positions in an array
Returning result.push
cuts your iteration short, and doesn't even include the index. Instead check if each element is equal to the needle, and then push the index if it's equal.
function indexOfValue(needle, hayStack) { let result = []; for(let i = 0; i < hayStack.length; i++) { if (hayStack[i] === needle) { // check if matching result.push(i); //push the index } } return result; //return result at end}console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]))
How to get a value from multiple arrays by the index?
You can use Array#map
to get a new array containing the fifth elements of each inner array. Note that arrays are zero-indexed, so the fifth value is at index 4
.
const BOARD = [
[ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
[ '5', '5', '5', '5', '5', '9', 'B', 'B', 'C', 'D', 'E', 'F', 'F', '1', '2', '3'],
[ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7'],
[ 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'],
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0'],
[ '5', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4'],
[ '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8'],
[ 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'],
[ '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1'],
[ '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5'],
[ 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
[ 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D'],
[ '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2'],
[ '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6'],
[ 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A'],
[ 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'],
];
let res = BOARD.map(r => r[4]);
console.log(res);
Related Topics
Javascript Add Class Active When Click a Link After Load Another Page
Html - Clicking a Button to Obtain Table Cell Value
How to Align Text Input Correctly in React Native
Check If a Key Exists Inside a Json Object
How to Execute a JavaScript Function from the Java Part of Jsp
Text Input Allow Only Integer Input in Angularjs
Getting Selected Values from Dropdown Inside Table
Mongodb, Update Collection Field If New Value Is Not Null
Deleting Nested Property in JavaScript Object
What Is the Max Size of Localstorage Values
Combining JavaScript and CSS into HTML File
How to Move the Mouse Pointer and Click With Selenium Webdriver JavaScript
Jspdf Library Cannot Insert Utf8 Letters into Pdf
Updating Value of Multidimensional Array of Object
Syntaxerror: Cannot Use Import Statement Outside a Module