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

4
  • Why have numbers at all.They are easily calculated from the hierarchy Commented Apr 2, 2020 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... Commented Apr 2, 2020 at 10:07
  • stackoverflow.com/questions/39887381/… have you checked this ? Commented Apr 2, 2020 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 Commented Apr 2, 2020 at 10:11

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.