Frage

In meinem JS, ich habe ein Objekt namens box_object. Es sieht wie folgt aus:

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

Nun möchte ich einige Positionswerte zu box_object.connectiondata_parent hinzuzufügen. Mit jQuery Ich kann die .each () -Methode verwenden. Also versuchte ich es, aber es ist fehlgeschlagen. In meiner Funktion muss ich die folgende:

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

Nach der Funktion meiner box_object sieht wie folgt aus:

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

Es scheint es nur die Werte an den ersten „Wert“ schreibt. Warum?

War es hilfreich?

Lösung

Nach einem Kommentar hier von Karl Swedberg , auf $(selector).each()

Dies sollte für die DOM-Elemente verwendet werden. Für normale Objekte oder Arrays verwenden jQuery.each () .

Vielleicht dann, das ist das, was Ihnen ein Problem geben.

Andere Tipps

Statt einen Rahmen für die einzelnen Funktionen zu verwenden, das folgende Codebeispiel wird die korrekten Einträge in den verschachtelten Elementen durchlaufen und die angeforderten Transformationen durchführen.

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 ");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top