Для пояснения: вам нужны различные методы в JavaScript, включающие функцию indexOf
с условием. Вот несколько примеров:
-
Использование
indexOf
с условием проверки наличия элемента в массиве:const array = [1, 2, 3, 4, 5]; const element = 3; if (array.indexOf(element) !== -1) { console.log("Element exists in the array"); } else { console.log("Element does not exist in the array"); }
-
Использование
indexOf
с условием для поиска первого вхождения подстроки в строке:const string = "Hello, world!"; const substring = "world"; if (string.indexOf(substring) !== -1) { console.log("Substring found in the string"); } else { console.log("Substring not found in the string"); }
-
Использование
indexOf
с условием для поиска индекса первого элемента массива, удовлетворяющего определенному условию:const array = [1, 2, 3, 4, 5]; const condition = (element) => element > 3; const index = array.findIndex(condition); if (index !== -1) { console.log("Index of the first element satisfying the condition:", index); } else { console.log("No elements satisfy the condition"); }