How to Search a Key Value into a Multidimensional Array in JS? Last Updated : 14 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To search for a key-value pair within a multidimensional array in JavaScript, you can use array iteration methods such as forEach(), some(), or filter(). Here are a few ways to do it effectively:1. Using a forEach() LoopThis approach iterates through each element of the array and checks if the desired key-value pair exists.Example: JavaScript const data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' } ]; function findKeyValue(arr, key, value) { let found = false; arr.forEach(item => { if (item[key] === value) { found = true; } }); return found; } console.log(findKeyValue(data, 'name', 'Bob')); // Output: true console.log(findKeyValue(data, 'name', 'David')); // Output: false Outputtrue false Explanation:forEach() iterates over each object in the array and checks if the given key exists with the specified value.2. Using Array.prototype.some()The some() method returns true if at least one element in the array satisfies the condition.Example: JavaScript const data = [ {id : 1, name : "Alice"}, {id : 2, name : "Bob"}, {id : 3, name : "Charlie"} ]; const exists = data.some(item => item.name === "Bob"); console.log(exists); // Output: true Outputtrue Explanation:some() is useful for checking if a condition is met without iterating through all elements once a match is found.In this example, item.name === 'Bob' checks if there is an object with the name key having the value 'Bob'.3. Using Array.prototype.filter()If you need to find all elements matching a key-value pair, you can use the filter() method.Example: JavaScript const data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Bob' } ]; const matchingItems = data.filter(item => item.name === 'Bob'); console.log(matchingItems); // Output: [ { id: 2, name: 'Bob' }, { id: 3, name: 'Bob' } ] Output[ { id: 2, name: 'Bob' }, { id: 3, name: 'Bob' } ] Explanation:filter() returns an array of all elements that satisfy the condition.In this example, it finds all objects with the name key set to 'Bob'.4. Handling Nested Structures with RecursionIf you have deeply nested structures (multidimensional arrays), you can use recursion to search for a key-value pair.Example: JavaScript const nestedData = [ { id: 1, name: 'Alice', info: [{ age: 30 }, { city: 'NYC' }] }, { id: 2, name: 'Bob', info: [{ age: 25 }, { city: 'LA' }] } ]; function searchKeyValue(array, key, value) { for (const item of array) { if (item[key] === value) { return true; } for (const prop in item) { if (Array.isArray(item[prop]) && searchKeyValue(item[prop], key, value)) { return true; } } } return false; } console.log(searchKeyValue(nestedData, 'city', 'NYC')); // Output: true console.log(searchKeyValue(nestedData, 'age', 40)); // Output: false Outputtrue false Explanation:This function uses recursion to search for a key-value pair in a multidimensional array, checking nested objects and arrays.SummaryBasic Arrays: Use some(), forEach(), or filter() for shallow searches.Nested Structures: Use recursion to search deeply nested arrays or objects.Adapt the approach depending on whether you need a boolean check (some()), an array of matches (filter()), or complex nested searches Comment More infoAdvertise with us Next Article How to get a key in a JavaScript object by its value ? A anuragtriarna Follow Improve Article Tags : JavaScript Web Technologies Web QnA Similar Reads How to Check if an Item Exists in a Multidimensional Array in JavaScript? To check if an item exists in a multidimensional array in JavaScript, you typically need to use a nested loop, or modern array methods such as Array.prototype.some(), to traverse each sub-array. Here are several ways to achieve this:1. Using Nested LoopsThe traditional way to check for an item in a 3 min read How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table 4 min read How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table 4 min read How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table 4 min read How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object 4 min read How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object 4 min read Like