Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> JavaScript

JavaScript में ऐरे को मल्टीपल स्ट्रिंग्स से कैसे फ़िल्टर करें?

उदाहरण

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' ]
  1. जावास्क्रिप्ट में ऑब्जेक्ट्स के ऐरे की प्रॉपर्टी नाम बदलना जावास्क्रिप्ट में ऑब्जेक्ट्स के ऐरे की प्रॉपर्टी नाम बदलना

    जावास्क्रिप्ट में ऑब्जेक्ट्स के ऐरे की प्रॉपर्टी नाम बदलने का कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width, initial-scale=1.0 /> <title>Document</title> <style>

  1. मल्टीपल वैल्यूज द्वारा जावास्क्रिप्ट ऐरे के एलिमेंट्स कैसे खोजें? मल्टीपल वैल्यूज द्वारा जावास्क्रिप्ट ऐरे के एलिमेंट्स कैसे खोजें?

    मल्टीपल वैल्यूज द्वारा जावास्क्रिप्ट ऐरे के एलिमेंट्स खोजने का कोड निम्नलिखित है - उदाहरण <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width, initial-scale=1.0 /> <title>Document</title> <style>

  1. JavaScript में फ्लैट ऑब्जेक्ट सरणी को ट्री में बदलना JavaScript में फ्लैट ऑब्जेक्ट सरणी को ट्री में बदलना

    मान लीजिए, हमारे पास ऑब्जेक्ट की एक सरणी है जैसे - const arr = [ { id: 1, name: name 1, parentId: null }, { id: 2, name: name 2, parentId: null }, { id: 2_1, name: name 2_1, parentId: 2 }, { id: 2_2, name: name 2_2, parentId: 2 }, { id: 3, name: name 3, parentId: null }, { i