Mendes
Mendes

Reputation: 18501

Javascript chained object: How to add an element to the end of the chain?

Consider the chained object below and the given code:

let data = {
    item: "0001",
    child: {
        item: "00011",
        child: {
            item: "000111",
            child: {
                item: "0001111",
                child: {
                    item: "00011111",
                    child: null
                }
            }
        }
    }
};

// Add item to last child of data
let last = data.child;
while (last !== null) last = chain.child;

// Add this item as last in chain
last = {
    item: "9999",
    child: null
};

console.log(data); // Same original data. No effect at all!!!

How can I add a new item in the last object child ?

Upvotes: 2

Views: 71

Answers (4)

enno.void
enno.void

Reputation: 6579

You can use recursion:

let data = {
    item: "0001",
    child: {
        item: "00011",
        child: {
            item: "000111",
            child: {
                item: "0001111",
                child: {
                    item: "00011111",
                    child: null
                }
            }
        }
    }
};

let run = function(node, addNode){
if(node.child === null){
    node.child = addNode
    return;
}

return run(node.child, addNode)
}

last = {
    item: "9999",
    child: null
};
run(data, last)
console.log(data); 

https://jsfiddle.net/q89vt6mn/3/

Upvotes: 0

Sunny Patel
Sunny Patel

Reputation: 8077

You probably meant to do the other way:

while (last.child !== null) last = last.child;

This will set you up so that last.child will be null, and you will be able to properly assign with last.child = {item: "9999", child: null};

Since you want to keep last as a reference pointer, you don't want to reassign it to a new object. This way, you navigate to the last child, and assign its child to your object.

Upvotes: 2

Sebastian Speitel
Sebastian Speitel

Reputation: 7346

You created a new object here instead of editing the current object in the variable last:

last = {
    item: "9999",
    child: null
};

If you want to change an attribute you need to use the . notation like

last.child = {item:"9999",child:null};

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386730

You need to check the next child element, because you need a child property to assign the object.

let data = {
    item: "0001",
    child: {
        item: "00011",
        child: {
            item: "000111",
            child: {
                item: "0001111",
                child: {
                    item: "00011111",
                    child: null
                }
            }
        }
    }
};

let last = data;                            // start with data
while (last.child !== null) {               // check the child
    last = last.child;                      // assig child for check of the child's child
}

last.child = { item: "9999", child: null }; // assign new object to a property for
                                            // keeping the reference

console.log(data); // Same original data. No effect at all!!!
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions