0

I have an existing empty array parentChildQuestions=[]. I'd like to add to it to build the following structure:

[
    {
        "id": "001",
        "childQuestions": [
            "002",
            "003"
        ]
    }
]

I have the following function but the childQuestions property doesn't seem to be adding. My log isn't showing it at all.

function addToDependentQuestions (thisQuestion) {
    if (parentChildQuestions.length===0) {
        var thisParent = parentChildQuestions.push({"id":thisQuestion.Parent_Question__c});
        thisParent.childQuestions=[thisQuestion.Id];

        console.log(thisParent.childQuestions);
    }
    else {
        var foundParentQuestion = $.grep(parentChildQuestions, function(e) { 
            return parentChildQuestions.Id == thisQuestion.Id;
        });
        foundParentQuestion.childQuestions.push(thisQuestion.Id);
    }

    console.log('parentChildQuestions' + JSON.stringify(parentChildQuestions));
}

1 Answer 1

2
var thisParent = parentChildQuestions.push({"id":thisQuestion.Parent_Question__c});

'array.push' does not return the newly added element. It returns the new length of the array.

http://www.w3schools.com/jsref/jsref_push.asp

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

1 Comment

There were actually a few errors in my original code, but this was the one causing the issue described

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.