javascript - Search an object from nested n level array / JSON object in java script

Javascript - Search an object from nested n level array / JSON object in java script

Searching for an object within a nested JSON structure in JavaScript typically involves recursively traversing the structure until the desired object is found. Here's a generic function to achieve this:

function findObjectByKey(obj, key, value) { if (typeof obj !== 'object' || obj === null) { return null; // Object is not valid or empty } if (obj[key] === value) { return obj; // Found the object with the matching key-value pair } for (var i in obj) { if (obj.hasOwnProperty(i)) { var foundObj = findObjectByKey(obj[i], key, value); // Recursively search nested objects if (foundObj !== null) { return foundObj; // Object found in nested structure } } } return null; // Object not found } 

This function takes three parameters:

  • obj: The JSON object or array to search within.
  • key: The key to search for within the objects.
  • value: The value to search for within the objects associated with the given key.

Here's how you can use this function:

var data = { id: 1, name: 'Parent', children: [ { id: 2, name: 'Child 1', children: [ { id: 3, name: 'Grandchild 1' } ] }, { id: 4, name: 'Child 2' } ] }; var foundObject = findObjectByKey(data, 'id', 3); console.log(foundObject); // Output: { id: 3, name: 'Grandchild 1' } 

This function will recursively search through the nested structure until it finds an object with the specified key-value pair. If no object is found, it returns null.

Examples

  1. Search nested object in JavaScript array

    • Description: This query implies the need to find a specific object within a nested array structure in JavaScript.
    function searchNestedObject(array, targetKey, targetValue) { for (let obj of array) { if (obj[targetKey] === targetValue) { return obj; } if (obj.children) { let result = searchNestedObject(obj.children, targetKey, targetValue); if (result) return result; } } return null; } 
  2. JavaScript find object in nested array

    • Description: This search is about locating an object within an array that contains nested arrays, typical of hierarchical data structures.
    function findObjectInNestedArray(arr, key, value) { return arr.find(obj => obj[key] === value || (obj.children && findObjectInNestedArray(obj.children, key, value))); } 
  3. Search JSON object in JavaScript

    • Description: Seeking methods to search for a particular JSON object within a JavaScript context.
    function searchJSONObject(jsonObject, key, value) { for (let obj of jsonObject) { if (obj[key] === value) { return obj; } if (obj.children) { let result = searchJSONObject(obj.children, key, value); if (result) return result; } } return null; } 
  4. Find object in nested JSON array JavaScript

    • Description: A query focusing on locating an object within a nested JSON array structure using JavaScript.
    function findObjectInNestedJSON(jsonArray, key, value) { for (let obj of jsonArray) { if (obj[key] === value) { return obj; } if (obj.children) { let result = findObjectInNestedJSON(obj.children, key, value); if (result) return result; } } return null; } 
  5. JavaScript search object in nested array of objects

    • Description: This query is about searching for a specific object within an array that contains nested objects.
    function searchObjectInNestedArray(arr, key, value) { return arr.find(obj => obj[key] === value || (obj.children && searchObjectInNestedArray(obj.children, key, value))); } 
  6. Search nested JSON object JavaScript

    • Description: Targeting the search for a nested JSON object within JavaScript data structures.
    function searchNestedJSONObject(jsonObj, key, value) { for (let obj of jsonObj) { if (obj[key] === value) { return obj; } if (obj.children) { let result = searchNestedJSONObject(obj.children, key, value); if (result) return result; } } return null; } 
  7. JavaScript find object in nested JSON

    • Description: Seeking methods to find an object within a JSON structure that contains nested objects, using JavaScript.
    function findObjectInNestedJSON(json, key, value) { for (let obj of json) { if (obj[key] === value) { return obj; } if (obj.children) { let result = findObjectInNestedJSON(obj.children, key, value); if (result) return result; } } return null; } 
  8. Search object in nested array JavaScript

    • Description: This query looks for approaches to search for a specific object within a nested array using JavaScript.
    function searchObjectInNestedArray(arr, key, value) { return arr.find(obj => obj[key] === value || (obj.children && searchObjectInNestedArray(obj.children, key, value))); } 
  9. Find nested object in JavaScript array

    • Description: Searching for a nested object within a JavaScript array, often used for complex data structures.
    function findNestedObjectInArray(array, key, value) { for (let obj of array) { if (obj[key] === value) { return obj; } if (obj.children) { let result = findNestedObjectInArray(obj.children, key, value); if (result) return result; } } return null; } 
  10. JavaScript search nested object

    • Description: This query is about searching for a nested object within JavaScript, typically within complex data structures.
    function searchNestedObject(array, targetKey, targetValue) { for (let obj of array) { if (obj[targetKey] === targetValue) { return obj; } if (obj.children) { let result = searchNestedObject(obj.children, targetKey, targetValue); if (result) return result; } } return null; } 

More Tags

android-navigation ntlm wampserver javafx-2 moment-timezone double oracle-apex-5 android-timepicker dto max-path

More Programming Questions

More Fitness-Health Calculators

More Trees & Forestry Calculators

More Tax and Salary Calculators

More Electronics Circuits Calculators