Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Get nested objects without recursion

Featured Replies

On my work i had the task to find values of nested objects. I solved it with recursion. I found this code in internet.

 

Demo

 

But our programmers said that it's not good to use recursion for that. Please, let me know if there is any other solution without recursion? I am very curious!

var data = {
      "Рыбы": {
        "Форель": {},
        "Щука": {}
      },

      "Деревья": {
        "Хвойные": {
          "Лиственница": {},
          "Ель": {}
        },
        "Цветковые": {
          "Берёза": {},
          "Тополь": {}
        }

      }
    };

    function createTree(container, obj) {
      container.innerHTML = createTreeText(obj);
    }

    function createTreeText(obj) { // отдельная рекурсивная функция
      var li = '';
      for (var key in obj) {
        li += '<li>' + key + createTreeText(obj[key]) + '</li>';
      }
      if (li) {
        var ul = '<ul>' + li + '</ul>'
      }
      return ul || '';
    }

    var container = document.getElementById('container');
    createTree(container, data);

Edited by fleur

  • Author

I didn't get actually.. but he said that recursion does to much request for site.. something like that. I closed my task in task manager but was said after his words. But if recursion is good i will continue use it in the future. :) Thank you for answer!

I didn't get actually.. but he said that recursion does to much request for site.. something like that. I closed my task in task manager but was said after his words. But if recursion is good i will continue use it in the future. :) Thank you for answer!

 

Unless we're not getting the full story here you just have some JSON data, that's a single request you made...

To move through it via recursion is pretty standard practice. Recursion isn't my strongpoint. I find it hard to follow when moving through complex tree structures, but this is where it is best suited.

 

It IS slower than using a loop but we're talking microseconds here so that kind of optimisation isn't important. With recursion we get access to the call stack which is handy for debugging. Just be aware if you have a stupidly deep tree you could overflow the stack, but that's highly unlikely (And is fixed in ES6)

Edited by rbrtsmith

  • Author

Thank you! i didn't see the real issues after using recursion, indeed. So.. i think i will continue use recursion for this kind of task (common task for me on this work).

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.