1

I have one array which collects objects. I want to implement insert operataion function which gets some path array "idsListPath" and the new object which we want to insert "itemToInsert".

EXAMPLE:

function InsertIntoDataSet(idsListPath, itemToInsert)
//idsListPath: the path where i want to add "itemToInsert" object
{
    //How do i push the itemToInsert in correct path?
}

SAMPLE DATA:

var idsListPath = ["1", "3", "44"];
var itemToInsert = { id: "111", node: [] };
var dataSet = [{
    id: "1", node: [
        {
            id: "98",
            node: []
        },
        {
            id: "3",
            node: [{
                id: "44",
                node: [] //TARGET LOCATION TO PUSH THE NEW ITEM
            }]
        }]
},
{
    id: "3",
    node: []
},
{
    id: "44",
    node: []
}];

As in sample our path is "1", "3", "44"...

So new object item should insert into dataSet of the object having id "1" > Its child node having id "3" > its child node having id "44" > under node [] i want to push the new object item.

How can i achieve this? Even if "idsListPath" array have single value that function should work.

2 Answers 2

2

Here is an implementation of InsertIntoDataSet that achieves what you need:

function InsertIntoDataSet(idsListPath, itemToInsert)
{
    var currentNode = dataSet;

    for(var idx in idsListPath) {
        var id = idsListPath[idx];

        if(currentNode = findChildArrayByNodeId(currentNode, id)) {
            continue;
        }
        else {
            return;
        }
    }

    if(currentNode) {
        currentNode.push(itemToInsert);
    }
}

function findChildArrayByNodeId(nodeArray, id) {
    var node = nodeArray.filter(function(el) {
        return el.id == id;
    })[0];

    return node ? node.node : undefined;
}

See live example.

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

Comments

2

All you have to do is pass the dataset as an argument to your function and amend it there.

Since object arguments are passed by-reference in JavaScript functions, you will update the initial object (not a copy of it).

In brief it could be something like:

function InsertIntoDataSet(idsListPath, itemToInsert, dataSet)
{
    if (!idsListPath.length) {
        // There are no more nodes to search for; insert.
        dataSet.insert(itemToInsert);
        return true;
    } else {
        dataSet.every(function (node) {
            if (node.id == idsListPath[0]) {
                idsListPath.splice(0, 1) // Remove the first element
                return InsertIntoDataSet(isdListPath, itemToInsert, node.node) // Recurse into the current node's dataset
            }
        })
    }
}
// Maybe you 'd like also to make a clone of idsListPath in each step in order not to amend the initial object.

EDIT: Corrected my answer, as I misunderstood the question at first.

2 Comments

Ok thanks for quick reply. How can i push that new object in exact location?>
The splice JavaScript function is what you need there actually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.