Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:48:01+00:00 2026-06-14T08:48:01+00:00

I have a very complex multidimensional array. Each item is like (this code shows

  • 0

I have a very complex multidimensional array.

Each item is like (this code shows the var that is pushed to the array):

var arrayItem = {
                'itemId': '00001',
                'itemName': 'Biscuits',
                'parentId': 'Food',
                'children': []
            };

itemId is always unique. The children array is populated with more arrayItem(s). The array can therefore have many levels (possible max 9, but maybe more) – So the base array has x number of objects and each of those can have y number of children objects and so on…

Is there a way to search for a particular itemId. I have tried various .each methods but I can’t seem to search all of the levels of the multidimentional array.

Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:48:02+00:00Added an answer on June 14, 2026 at 8:48 am

    Edit:

    A few years worth of experience and Array prototype improvements later, here’s how I’d solve the problem:

    function find(arr, id) {
        return arr.find(item => item.itemId === id) 
            || arr.reduce((found, curr) => found || find(curr.children, id), undefined)
    }
    

    This performs a breadth-first search through the object graph, and returns undefined if it does not contain the ID you’re looking for.

    Original answer:

    function finder(arr, id){
        arr = [].concat(arr);
        for(var i = 0; i<arr.length; i++){
            if(arr[i].itemId == id){
                return arr[i];
            }else if(arr[i].children.length){
                var val = finder(arr[i].children, id);
                if(val){
                    return val;
                }
            }
        }
        return null;        
    }
    

    Pass finder your array and the desired ID, and it will find and return the array item with that id.

    Use it like this:

    var arr = [{
        'itemId': '00001',
            'itemName': 'Biscuits',
            'parentId': 'Food',
            'children': [{
            'itemId': '00002',
                'itemName': 'Sweet',
                'parentId': 'Biscuits',
                'children': [{
                'itemId': '00004',
                    'itemName': 'Chocolate',
                    'parentId': 'Sweet',
                    'children': []
            }]
        }, {
            'itemId': '00003',
                'itemName': 'Salty',
                'parentId': 'Biscuits',
                'children': []
        }]
    }];
    
    var chocolate = finder(arr,"00004");
    

    Demo here: http://jsfiddle.net/mVyha/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a very complex Oracle package here (over 4,100 lines of code) that's
We have a very complex config file for JSF managed beans that looks like
I have a very complex Microsoft Access report. This report is run for multiple
I have a very complex task - create a software that imports XMl files
I have a very complex graph, and there is no way that I can
I have a very complex CTE and I would like to insert the result
Here is the problem: I have a very complex plugin that does lots of
I have a very complex Linq to SQL query that returns a result set
I have a very complex issue that Me and my coworker have been discussing
I've worked on projects that have very complex XML configuration, and one problem that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.