我有一个现有的空数组parentChildQuestions=[]
。我想添加它来构建以下结构:
[
{
"id": "001",
"childQuestions": [
"002",
"003"
]
}
]
我有以下功能,但childQuestions
属性似乎没有添加。我的日志根本没有显示。
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));
}
答案 0 :(得分:2)
var thisParent = parentChildQuestions.push({"id":thisQuestion.Parent_Question__c});
'的Array.push'不返回新添加的元素。它返回数组的新长度。