Question

Dans mon JS, j'ai un objet appelé box_object. Il ressemble à ceci:

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

Maintenant, je veux ajouter quelques valeurs de position à box_object.connectiondata_parent. En utilisant jQuery je peux utiliser la méthode .each (). Alors je l'ai essayé, mais il a échoué. Dans ma fonction, je fais ce qui suit:

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

Une fois la fonction de mes regards box_object comme ceci:

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

Il semble qu'il écrit que les valeurs de la première « valeur ». Pourquoi?

Était-ce utile?

La solution

Selon un commentaire par Karl Swedberg , sur $(selector).each()

  

Il doit être utilisé pour les éléments DOM.   Pour les objets normaux ou des tableaux, utilisez    jQuery.each () .

Peut-être alors qui est ce qui vous donne un problème.

Autres conseils

Au lieu d'utiliser un cadre pour la chaque fonctionnalité, l'exemple de code suivant itérer sur les entrées appropriées dans les éléments imbriqués et effectuer les transformations demandées.

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 ");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top