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 Search a Key Value into a Multidimensional Array in JS? 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 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 Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met 5 min read How to get the Value by a Key in JavaScript Map? JavaScript Map is a powerful data structure that provides a convenient way to store key-value pairs and retrieve values based on keys. This can be especially useful when we need to associate specific data with unique identifiers or keys.Different Approaches to Get the Value by a Key in JavaScript Ma 3 min read How to get Values from Specific Objects an Array in JavaScript ? In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filte 2 min read How to get the Index of an Array based on a Property Value in TypeScript ? Getting the index of an array based on a property value involves finding the position of an object within the array where a specific property matches a given value. The below approaches can be used to accomplish the task. Table of Content Using Array.findIndex()Using reduce() methodUsing for loopUsi 4 min read How to Search for Multiple Words Within a String or Array in JavaScript? To search for multiple words within a string or an array in JavaScript, you can use different approaches depending on the data structure. Hereâs how to perform the search effectively:1. Searching for Multiple Words in a StringTo check if a string contains multiple words, you can use String.prototype 3 min read JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde 2 min read How to check whether an array includes a particular value or not in JavaScript ? In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The followin 3 min read Like