How to get the object based on the id in a multidimensional array of objects?

Related searches

Hello I am trying to figure out how to get the object with a specific id from an array with mutlitple levels. The array will have objects, these objects may have arrays with objects as values. For example see the data given below:

[
    {
        "id": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8",
        "text": "Parent Menu 2",
        "icon": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8/rose-3142529__340.jpg",
        "page_type": "parent_menu",
        "data": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8",
        "ui_config": "",
        "order": 2
    },
    {
        "id": "5bc4ff1a-69b5-496b-a484-0aa99b886122",
        "text": "Home",
        "icon": "5bc4ff1a-69b5-496b-a484-0aa99b886122/1024x1024.png",
        "page_type": "parent_menu",
        "is_homepage": true,
        "data": "5bc4ff1a-69b5-496b-a484-0aa99b886122",
        "ui_config": "",
        "order": 1,
        "submenus": [
            {
                "id": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e",
                "text": "Grid Menu",
                "icon": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e/rose-3142529__340.jpg",
                "page_type": "grid_menu",
                "data": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e",
                "ui_config": "",
                "order": 1,
                "submenus": [
                    {
                        "id": "906cd86f-df57-4175-a2c6-ca3a5c724e8a",
                        "text": "Article Page 2",
                        "icon": "906cd86f-df57-4175-a2c6-ca3a5c724e8a/rose-3142529__340.jpg",
                        "page_type": "article_page",
                        "data": "{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}",
                        "ui_config": "",
                        "order": 2
                    },
                    {
                        "id": "da8b054a-274e-4e1b-9d5a-e058a934838b",
                        "text": "Article Page",
                        "icon": "da8b054a-274e-4e1b-9d5a-e058a934838b/rose-3142529__340.jpg",
                        "page_type": "article_page",
                        "data": "{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}",
                        "ui_config": "",
                        "order": 1
                    }
                ]
            }
        ]
    }
]

Here lets say I need to get the object with id da8b054a-274e-4e1b-9d5a-e058a934838b. How do I do that?

Here is a recurisve function I wrote for this:

var currentMenuItem = null
function getTheCurrentMenu(menu, id) {
  if (currentMenuItem != null) {
    return
  }
  if (!Array.isArray(menu)) {
    if (menu.id == id) {
      currentMenuItem = menu
      return
    }
  }
  for (var item in menu) {
    console.log(item)
    getTheCurrentMenu(item, id)
  }
}

This makes the browser unresponsive and shows error too much recursion.

Can someone please help me with this?

You can do this using reduce method and set accumulator to current element if id matches.

const data = [{"id":"e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8","text":"Parent Menu 2","icon":"e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8/rose-3142529__340.jpg","page_type":"parent_menu","data":"e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8","ui_config":"","order":2},{"id":"5bc4ff1a-69b5-496b-a484-0aa99b886122","text":"Home","icon":"5bc4ff1a-69b5-496b-a484-0aa99b886122/1024x1024.png","page_type":"parent_menu","is_homepage":true,"data":"5bc4ff1a-69b5-496b-a484-0aa99b886122","ui_config":"","order":1,"submenus":[{"id":"4960a54c-6d27-4d8d-a272-ebc7d9e8a55e","text":"Grid Menu","icon":"4960a54c-6d27-4d8d-a272-ebc7d9e8a55e/rose-3142529__340.jpg","page_type":"grid_menu","data":"4960a54c-6d27-4d8d-a272-ebc7d9e8a55e","ui_config":"","order":1,"submenus":[{"id":"906cd86f-df57-4175-a2c6-ca3a5c724e8a","text":"Article Page 2","icon":"906cd86f-df57-4175-a2c6-ca3a5c724e8a/rose-3142529__340.jpg","page_type":"article_page","data":"{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}","ui_config":"","order":2},{"id":"da8b054a-274e-4e1b-9d5a-e058a934838b","text":"Article Page","icon":"da8b054a-274e-4e1b-9d5a-e058a934838b/rose-3142529__340.jpg","page_type":"article_page","data":"{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}","ui_config":"","order":1}]}]}]

function get(data, id) {
  return data.reduce((r, e) => {
    if(e.id === id) r = e;
    if(!r && e.submenus) return get(e.submenus, id)
    return r;
  }, null)
}

const result = get(data, 'da8b054a-274e-4e1b-9d5a-e058a934838b');
console.log(result)

How to find the max id in an array of objects in JavaScript, I had this array of objects and in each object I had an id. My main objective every time I write code is to get something to work first and then� If you want to use the plain old javascript you can create a function that iterates over the array and then return the object when the id matches with the one you’re looking for (or return null

You could take an iterative and recursive call with a short circuit, if the item is found.

function find(array, id) {
    var item;
    for (item of array) {
        if (item.id === id) return item;
        if (item.submenus && (item = find(item.submenus, id))) return item;
    }
}

var data = [{ id: "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8", text: "Parent Menu 2", icon: "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8/rose-3142529__340.jpg", page_type: "parent_menu", data: "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8", ui_config: "", order: 2 }, { id: "5bc4ff1a-69b5-496b-a484-0aa99b886122", text: "Home", icon: "5bc4ff1a-69b5-496b-a484-0aa99b886122/1024x1024.png", page_type: "parent_menu", is_homepage: true, data: "5bc4ff1a-69b5-496b-a484-0aa99b886122", ui_config: "", order: 1, submenus: [{ id: "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e", text: "Grid Menu", icon: "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e/rose-3142529__340.jpg", page_type: "grid_menu", data: "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e", ui_config: "", order: 1, submenus: [{ id: "906cd86f-df57-4175-a2c6-ca3a5c724e8a", text: "Article Page 2", icon: "906cd86f-df57-4175-a2c6-ca3a5c724e8a/rose-3142529__340.jpg", page_type: "article_page", data: "{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}", ui_config: "", order: 2 }, { id: "da8b054a-274e-4e1b-9d5a-e058a934838b", text: "Article Page", icon: "da8b054a-274e-4e1b-9d5a-e058a934838b/rose-3142529__340.jpg", page_type: "article_page", data: "{\"page_id\":\"3e025c87-1d03-46a6-800e-0a9daea52bf4\"}", ui_config: "", order: 1 }] }] }],
    id = 'da8b054a-274e-4e1b-9d5a-e058a934838b',
    result = find(data, id);

console.log(result);

array_column - Manual, A multi-dimensional array or an array of objects from which to pull a column of Example #2 Get the column of last names from a recordset, indexed by the "id" column the column of usernames from the public "username" property of an object of PHP, I wrote my own version and extended it a little based on my needs. So now simply pass “id” in place of key and “45” in place of value, and you will get the full object matching an id of 45. So that would be, myArr.filterObjects ("id", "45");

var data = `
[
    {
        "id": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8",
        "text": "Parent Menu 2",
        "icon": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8/rose-3142529__340.jpg",
        "page_type": "parent_menu",
        "data": "e1f015d3-4ca8-4dfd-839d-bed9bbbb82f8",
        "ui_config": "",
        "order": 2
    },
    {
        "id": "5bc4ff1a-69b5-496b-a484-0aa99b886122",
        "text": "Home",
        "icon": "5bc4ff1a-69b5-496b-a484-0aa99b886122/1024x1024.png",
        "page_type": "parent_menu",
        "is_homepage": true,
        "data": "5bc4ff1a-69b5-496b-a484-0aa99b886122",
        "ui_config": "",
        "order": 1,
        "submenus": [
            {
                "id": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e",
                "text": "Grid Menu",
                "icon": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e/rose-3142529__340.jpg",
                "page_type": "grid_menu",
                "data": "4960a54c-6d27-4d8d-a272-ebc7d9e8a55e",
                "ui_config": "",
                "order": 1,
                "submenus": [
                    {
                        "id": "906cd86f-df57-4175-a2c6-ca3a5c724e8a",
                        "text": "Article Page 2",
                        "icon": "906cd86f-df57-4175-a2c6-ca3a5c724e8a/rose-3142529__340.jpg",
                        "page_type": "article_page",
                        "ui_config": "",
                        "order": 2
                    },
                    {
                        "id": "da8b054a-274e-4e1b-9d5a-e058a934838b",
                        "text": "Article Page",
                        "icon": "da8b054a-274e-4e1b-9d5a-e058a934838b/rose-3142529__340.jpg",
                        "page_type": "article_page",
                        "ui_config": "",
                        "order": 1
                    }
                ]
            }
        ]
    }
]`;


var menu = JSON.parse(data);

async function getTheCurrentMenu(menu, id) {
  if (!Array.isArray(menu)) {
    if (menu.id == id) {
      return menu;
    } else if(menu.submenus){
      return getTheCurrentMenu(menu.submenus, id);
    } else {
      return;
    }
  }
  for (let item of menu) {
    console.log(item)
    let result = await getTheCurrentMenu(item, id);
    if(result != null) {
      return result;
    }
  }
}

getTheCurrentMenu(menu, 'da8b054a-274e-4e1b-9d5a-e058a934838b').then((item) => {
  console.log("Result: ", item);
})

5. Working with Arrays and Loops, In JavaScript, an array can be created using formal object notation, or it … Arrays in JavaScript are zero-based, which means the first element index is zero, and the last You want to create a multidimensional array (an array of arrays). Each of those three elements is also created as Array objects with three members . Distinct objects by property value from an array of objects. Sometimes we want to get an array of distinct objects by property value from the original array. For example, we have an array of objects as below. For each object, id and name properties are in 1:1 relationship.

How to use PHP array functions instead of loops, They're used by PHP experts to make their life easier and replace a lot of instances where A vector is a one-dimensional array and matrix is a multidimensional array. Like object-oriented programming, this isn't an easy topic to cover. We get each permalink by calling the get_post_permalink function with the post ID. The following code example demonstrates how to set and get a specific value in a one-dimensional or multidimensional array. using namespace System; int main() { // Creates and initializes a one-dimensional array. array<String^>^myArr1 = gcnew array<String^>(5); // Sets the element at index 3.

Arrays, It is not convenient to use an object here, because it provides no methods to manage the order of elements. We can get an element by its number in square brackets: That's possible, because arrays are objects at their base. We can use it for multidimensional arrays, for example to store matrices:. The array has only 1 element and the element is the object (denoted by the curly braces “{ }”) containing three properties. To get these values, you access them like this:

1.1. get() Syntax public Object get( int index ); 1.2. get() Parameter. index – index of the element to return. A valid index will always be between 0 (inclusive) to the size of ArrayList (exclusive). For example, if ArrayList holds 10 objects than a valid argument index will be between 0 to 9 (both inclusive).

Comments
  • Using [].some with side effect is bad practice. Using a for..of loop here is much more idiomatic.
  • @MadaraUchiha, is it better now?
  • @NinaScholz will this work, if it has say like 5 levels deep submenus?
  • @defiant, it should. please try it with your data.
  • It didn't work with my data. The other answer did. Don't know why it didn even though it works here.