1
storeChildren : function(coreid, data) {
  $.each(data, function(id, type) {
    $.core.children[coreid] = {};
    $.core.children[coreid]['id'] = id;
    $.core.children[coreid]['type'] = type;
  });
}

($ .core.childrenはオブジェクトです)

結果の配列/オブジェクト(私はオブジェクトが好きです)を次のようにしたいと思います(私のPHP擬似コードを失礼します):

array(coreid => array(id=>type, id=>type, id=>type));

ループを実行するたびに$.core.children[coreid]を上書きしていることはわかっていますが、修正方法がわかりません。

4

2 に答える 2

2

すべての'idが一意である場合、以下が機能するはずです。これはあなたが求めているものですか?

storeChildren : function(coreid, data) {
  $.core.children[coreid] = {};
  $.each(data, function(id, type) {
    $.core.children[coreid][id] = type;
  });
}
于 2012-04-07T04:42:23.327 に答える
0
init: function() {
    $.core.children = []; // instantiate array
},

storeChildren : function(coreid, data) {

    $.each(data, function(id, type) {
        $.core.children[coreid] = {id : type};
    });
}

コード内の他の場所でオブジェクトをインスタンス化すると、連想配列/オブジェクトを毎回強制終了することはありません。上記の例でも、配列の代わりに配列内のオブジェクトを使用しています。結果は、JSON表記で次のようになります。

[ {key,value} , {key,value} , {key,value} , ...]

coreidは配列のインデックスです。

于 2012-04-07T04:45:16.800 に答える