उदाहरण
for लूप और indexOf() का उपयोग करके मल्टीपल स्ट्रिंग्स से ऐरे फ़िल्टर करें।
var details = [
'My first Name is John and last Name is Smith',
'My first Name is John and last Name is Doe',
'Student first Name is John and last Name is Taylor'
];
var isPresent;
var records = [];
var matchWords = ['John', 'Doe'];
for (var index = 0; index < details.length; index++) {
isPresent = true;
for (var outer = 0; outer < matchWords.length; outer++) {
if (details[index].indexOf(matchWords[outer]) === -1) {
isPresent = false;
break;
}
}
if (isPresent) {
records.push(details[index]);
}
}
console.log(records);
प्रोग्राम चलाने के लिए:
node fileName.js
आउटपुट
PS C:\Users\Amit\JavaScript-code> node demo151.js [ 'My first Name is John and last Name is Doe' ]