Skip to content

Instantly share code, notes, and snippets.

@psandeepunni
Last active November 18, 2021 22:08
Show Gist options
  • Save psandeepunni/80f5a62f0540b7989729 to your computer and use it in GitHub Desktop.
Save psandeepunni/80f5a62f0540b7989729 to your computer and use it in GitHub Desktop.
Javascript function to flatten a nested Associative Array (tree) to a List
var bfs = function(tree, key, collection) {
if (!tree[key] || tree[key].length === 0) return;
for (var i=0; i < tree[key].length; i++) {
var child = tree[key][i]
collection[child.id] = child;
bfs(child, key, collection);
}
return;
}
// Sample Usage
var flattenedCollection = {};
var dataTree = {"children" : [
{"id" : "1", "name" : "A", "children" : [{"id" : 11, "name" : "AA"},{"id" : 12, "name" : "AB"}]},
{"id" : "2", "name" : "B", "children" : [{"id" : 21, "name" : "BA", "children" : [{"id":"211","name":"BAC"}]},{"id" : 22, "name" : "BB"}]},
]};
bfs(dataTree, "children", flattenedCollection);
console.log(flattenedCollection["211"].name);
@maqsoodsyed1995
Copy link

Incase if someone wants just the leaf nodes,

var mockData = [];

  mockData.push({
    item: {
      id: "id3",
      label: "Lorem ipsum dolor 3",
      value: 1,
      checked: true
    },
    children: [
      {
        item: {
          id: "id31",
          label: "Lorem ipsum dolor 31",
          value: 1,
          checked: true
        },
        children: [
          {
            item: {
              id: "id321",
              label: "Item 1",
              value: 1,
              checked: true
            }
          },
          {
            item: {
              id: "id322",
              label: "Item 2",
              value: 1,
              checked: true
            }
          }
        ]
      },
      {
        item: {
          id: "id32",
          label: "Lorem ipsum dolor 32",
          value: 1,
          checked: true
        },
        children: [
          {
            item: {
              id: "id321",
              label: "Item 3",
              value: 1,
              checked: true
            }
          },
          {
            item: {
              id: "id322",
              label: "Item 4",
              value: 1,
              checked: true
            },
            children: [
              {
                item: {
                  id: "id321",
                  label: "Item 3",
                  value: 1,
                  checked: true
                }
              },
              {
                item: {
                  id: "id322",
                  label: "Item 4",
                  value: 1,
                  checked: true
                }
              }
            ]
          }
        ]
      }
    ]
  });

  $(function() {
    $("#tree-container").checkTree({
      data: mockData
    });
  });
  let flattenedCollection = [];
  let bfs = function(tree, key, collection) {
    if (!tree[key] || tree[key].length === 0) {
      collection.push(tree);
      return;
    }
    for (var i = 0; i < tree[key].length; i++) {
      var child = tree[key][i];
      bfs(child, key, collection);
    }
    return;
  };
  bfs(mockData[0], "children", flattenedCollection);
  console.log(flattenedCollection);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment