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

JavaScript में केस-इनसेंसिटिव सबस्ट्रिंग से ऑब्जेक्ट्स की ऐरे फिल्टर कैसे करें?

filter() मेथड के साथ toLowerCase() या toUpperCase() का उपयोग करके केस-इनसेंसिटिव (बड़े/छोटे अक्षर नजरअंदाज करके) सबस्ट्रिंग मिलान करके ऑब्जेक्ट्स की ऐरे फिल्टर करें।

डेटा सेटअप

const students = [
    { studentName: "John Smith" },
    { studentName: "john smith" },
    { studentName: "Carol Taylor" },
    { studentName: "JOHN SMITH" },
    { studentName: "David Johnson" }
];

विधि 1: toLowerCase() के साथ indexOf()

const searchName = "John Smith";

const results = students.filter(obj => 
    obj.studentName.toLowerCase().indexOf(searchName.toLowerCase()) >= 0
);

console.log(results);
// आउटपुट: 
// [ { studentName: 'John Smith' }, 
//   { studentName: 'john smith' }, 
//   { studentName: 'JOHN SMITH' } ]

विधि 2: toLowerCase() के साथ includes() (मॉडर्न, अधिक पठनीय)

const searchName = "john";

const results = students.filter(obj => 
    obj.studentName.toLowerCase().includes(searchName.toLowerCase())
);

console.log(results);
// सभी जॉन नाम वाले मिलेंगे

विधि 3: रेगुलर एक्सप्रेशन के साथ (शक्तिशाली)

const searchTerm = "john smith";
const regex = new RegExp(searchTerm, "i"); // "i" फ्लैग = केस-इनसेंसिटिव

const results = students.filter(obj => regex.test(obj.studentName));
console.log(results);

पुन: प्रयोज्य फंक्शन

function filterBySubstring(array, property, searchTerm) {
    if (!searchTerm) return array; // खोज खाली तो सभी लौटाएं
    
    const term = searchTerm.toLowerCase();
    return array.filter(item => 
        item[property] && item[property].toLowerCase().includes(term)
    );
}

// उपयोग
const filtered = filterBySubstring(students, "studentName", "JOHN");
console.log(filtered);

चलाने का तरीका

node filter-demo.js

आउटपुट

[
  { studentName: 'John Smith' },
  { studentName: 'john smith' },
  { studentName: 'JOHN SMITH' }
]

प्रदर्शन टिप

बड़ी ऐरे के लिए, खोज टर्म को एक बार toLowerCase() करें, लूप के अंदर नहीं:

// ✅ अच्छा - खोज टर्म एक बार लोअरकेस
const term = searchName.toLowerCase();
const results = students.filter(obj => obj.studentName.toLowerCase().includes(term));

// ❌ खराब - हर इटरेशन में लोअरकेस
const results = students.filter(obj => obj.studentName.toLowerCase().includes(searchName.toLowerCase()));
  1. - JavaScript में दूसरे ऐरे से एसोसिएटिव ऐरे को फ़िल्टर करना

    मान लीजिए, हमारे पास ऑब्जेक्ट्स के दो ऐरे हैं:const data = [    {"XD_A":"XDL","XD_B_1":"38","XD_B_2":"PB"},    {"XD_A":"XDR","XD_B_1":"51","XD_B_2":"PB"},  

  1. - JavaScript में यादृच्छिक स्ट्रिंग के आधार पर सरणी के स्ट्रिंग्स बनाना और मिलान करना

    मान लीजिए, हमारे पास स्ट्रिंग्स की एक सरणी है जिसमें कुछ नाम हैं जैसे: const arr = [Dinesh, Mahesh, Rohit, Kamal, Jatin Sapru, Jai]; और वर्णों की एक यादृच्छिक स्ट्रिंग जैसे: const str = lsoaakjm; हमें एक JavaScript फ़ंक्शन लिखना है जो ऐसी सरणी और स्ट्रिंग को दो आर्गुमेंट के रूप में लेता है। फिर फ़ंक

  1. - JavaScript में सरणी से मिलान करने वाली जोड़ी ढूंढना

    हमें एक JavaScript फ़ंक्शन लिखना है जो पूर्णांकों की एक सरणी लेता है जिसमें कुछ दोहराव वाले मान हो सकते हैं। हमारे फ़ंक्शन को सरणी से समान पूर्णांकों के जोड़ों की संख्या ढूंढनी चाहिए जिन्हें हम निकाल सकते हैं। उदाहरण यदि इनपुट सरणी है: const arr = [1, 5, 2, 1, 6, 2, 2, 9]; तो आउटपुट होना चाहिए: cons