एकाधिक स्ट्रिंग्स द्वारा सरणी फ़िल्टर करने के लिए, 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.
यहाँ, मेरी फ़ाइल का नाम है demo151.js.
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
PS C:\Users\Amit\JavaScript-code> node demo151.js [ 'My first Name is John and last Name is Doe'