English 中文(简体)
JS: Object iteration der
原标题:JS: Object iteration fails

In my JS, I have an object called box_object. It looks like this:

({  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"}}
})

Now, I want to add some position values to box_object.connectiondata_parent. Using jQuery I can use the .each() method. So I tried it, but it failed. In my function I do the following:

$(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{}
});

在行使职能之后,我的方框“目标”认为:

({ 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"}}
})

这似乎只是把价值写到第一“价值”。 为什么?

最佳回答
问题回答

下面的编码样本将不使用每个功能的框架,而是将分散在封顶元素的适当条目上,并完成所要求的转变。

function assert(cond, msg) {
  if (!cond) {
    throw msg + " ... failed";
  }
}

// assumed globals
var startingpoint_left   = 500;
var startingpoint_top    = 104;
var startingpoint_bottom =  50; // never shown in sample but referenced                                                         
var idx;

for (idx in box_object.connectiondata_parent) {
  if (box_object.connectiondata_parent.hasOwnProperty(idx)) {
    if (box_object.connectiondata_parent[idx]) {
      box_object.connectiondata_parent[idx].position = {
        "left": startingpoint_left,
        "top":  box_object.connectiondata_parent[idx].linepoint === "top" ? startingpoint_top : startingpoint_bottom
      };
    }
  }
}

assert(box_object.connectiondata_parent[0].position.top  === 104, "index 0 top ");
assert(box_object.connectiondata_parent[0].position.left === 500, "index 0 left");
assert(box_object.connectiondata_parent[1].position.top  === 104, "index 1 top ");
assert(box_object.connectiondata_parent[1].position.left === 500, "index 1 top ");




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签