在我的 JS 中,我有一个名为 box_object 的对象。它看起来像这样:
({ id:"3",
text:"this is a box object",
connection_parent:["1", "2"],
connection_child:["5", "6"],
connectiondata_child:{
0:{id:"5", linepoint:"bottom"},
1:{id:"6", linepoint:"bottom"}},
connectiondata_parent:{
0:{id:"1", linepoint:"top"},
1:{id:"2", linepoint:"top"}}
})
现在,我想向 box_object.connectiondata_parent 添加一些位置值。使用 jQuery,我可以使用 .each() 方法。所以我尝试了它,但它失败了。在我的功能中,我执行以下操作:
$(box_object.connectiondata_parent).each(function(it, obj){
if(typeof(obj[it]) != "undefined" && obj[it].linepoint == "top"){
var point_position_top = new Object();
point_position_top.left = startingpoint_left;
point_position_top.top = startingpoint_top;
obj[it].position = point_position_top;
}else if(typeof(obj[it]) != "undefined" && obj[it].linepoint == "bottom"){
var point_position_bottom = new Object();
point_position_bottom.left = startingpoint_left;
point_position_bottom.top = startingpoint_bottom;
obj[it].position = point_position_bottom;
}else{}
});
在函数之后,我的 box_object 看起来像这样:
({ id:"3",
text:"this is third box",
connection_parent:["1", "2"],
connection_child:["5", "6"],
connectiondata_child:{
0:{id:"5", linepoint:"bottom"},
1:{id:"6", linepoint:"bottom"}},
connectiondata_parent:{
0:{id:"1", linepoint:"top", position:{left:500, top:104}},
1:{id:"2", linepoint:"top"}}
})
它似乎只将值写入第一个“值”。为什么?