0

I got the following array:

let data =  [
        {"title": {number: 1},
            "children": [
                {"title": {number: 1.1}},
                {"title": {number: 1.2}},
                {"title": {number: 1.3}},
                {"title": {number: 1.4}},
                {"title": {number: 1.5}}
            ]
        },
        {"title": {number: 2},
            "children": [
                {"title":{number: 2.1}},
                {"title":{number: 2.2},
                    "children": [
                        {"title":{number: 2.2.1}},
                        {"title":{number: 2.2.2}},
                        {"title":{number: 2.2.3}},
                        {"title":{number: 2.2.4}} 
                    ]
                },
                {"title":{number: 2.3}},
                {"title":{number: 2.4}}
            ]
        },
        {"title": {number: 3},
            "children": []
        }
    ];

Every element of this array may change its place, new element may be added, existing element may be removed. The task is to recalculate elements numbers in order to maintain their correct order. For example, if element with number 2.2.2 is removed, the other elements become 2.2.1, 2.2.2 and 2.2.3. If some element is removed with its children, numbers for all other elements shoud also be recalculated. Any ideas how to do that would be welcome. Thank you

Masha
  • 827
  • 1
  • 10
  • 30
  • Why have numbers at all.They are easily calculated from the hierarchy – mplungjan Apr 02 '20 at 10:03
  • Any ideas how to find them out from hierarchy? As far as I understand I need to recursively keep track of nesting level and add increment for relevant element... – Masha Apr 02 '20 at 10:07
  • https://stackoverflow.com/questions/39887381/how-to-get-nested-array-length-in-javascript have you checked this ? – sylvann Apr 02 '20 at 10:07
  • Yes, you need to recurse. but as you see `data[0].title` is the first title, `data[1]title` the second. `data[1].children[0]` is 2.1 etc – mplungjan Apr 02 '20 at 10:11

1 Answers1

1

You could create recursive function that you will call every time your object structure changes to recalculate the positions of each element in the tree.

function recalc(data, prev = '') {
  data.forEach((e, i) => {
    let dot = prev ? '.' : '';
    let number = prev + dot + (i + 1);

    if (e.children) {
      recalc(e.children, number)
    }

    if (e.title) {
      e.title.number = number
    }
  })
}

const data1 = [{"title":{"number":1},"children":[{"title":{"number":1.1}},{"title":{"number":1.2}},{"title":{"number":1.3}},{"title":{"number":1.4}},{"title":{"number":1.5}}]},{"title":{"number":2},"children":[{"title":{"number":2.1}},{"title":{"number":2.2},"children":[{"title":{"number":"2.2.1"}},{"title":{"number":"2.2.2"}},{"title":{"number":"2.2.3"}},{"title":{"number":"2.2.4"}}]},{"title":{"number":2.3}},{"title":{"number":2.4}}]},{"title":{"number":3},"children":[]}]
recalc(data1);
console.log(data1)

const data2 = [{"title":{"number":1},"children":[{"title":{"number":1.1}},{"title":{"number":1.2}},{"title":{"number":1.5}}]},{"title":{"number":2},"children":[{"title":{"number":2.1}},{"title":{"number":2.2},"children":[{"title":{"number":"2.2.3"}},{"title":{"number":"2.2.4"}}]},{"title":{"number":2.3}},{"title":{"number":2.4}}]},{"title":{"number":3},"children":[{title: {}, children: [{title: {}}, {title: {}}]}]}]
recalc(data2);
console.log(data2)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176