How to check if array of strings contains a substring in Node.js

2 Answers

0 votes
const array = ['c++', 'node.js', 'c', 'java'];
const substring = 'js';
 
const match = array.find(element => {
    if (element.includes(substring)) {
        return true;
    }
});
 
console.log(match); 
 
 
 
 
 
/*
run:
 
node.js
 
*/

 



answered Feb 5, 2022 by avibootz
0 votes
const array = ['c++', 'node.js', 'c', 'java'];
const substring = 'js';
 
const index = array.findIndex(element => {
    if (element.includes(substring)) {
        return true;
    }
});
 
console.log(index); 
 
 
 
 
/*
run:
 
1

*/

 



answered Feb 5, 2022 by avibootz
...