0

次のように、タイプと名前アドレスなどの2つの配列を含むモデルがあります。

  var model = [{"bstype":1},{"bstype":2},{"bstype":3},{"bstype":4}],
  [{"bstype":1, "name":"John","Address":"Sample address"}, 
  [{"bstype":1, "name":"John","Address":"Sample address"},
  [{"bstype":3, "name":"John","Address":"Sample address"},
  {"bstype":2 ,"name":"John","Address":"Sample address"}];
  [{"bstype":2, "name":"John","Address":"Sample address"},
  [{"bstype":4, "name":"John","Address":"Sample address"}];

私がしたいのは、次のリストを作成することです。

何かのようなもの

   I am not sure about this part how to implement it that is why it was gibberish.
   bstype":1 will have a view of following
     [{"bstype":1, "name":"John","Address":"Sample address"}, 
     [{"bstype":1, "name":"John","Address":"Sample address"},
   bstype"2: will have a view of following
     {"bstype":2 ,"name":"John","Address":"Sample address"}];
     [{"bstype":2, "name":"John","Address":"Sample address"},
   bstype":3 has only one
     [{"bstype":3, "name":"John","Address":"Sample address"},

などなど。

私はノックアウトを使用しています。サイトをチェックしましたが、foreachについてのみ説明しており、子要素へのアクセス方法については説明していません。

これが理にかなっていることを願っています。

ありがとう

4

2 に答える 2

1

同じキーで行ったように、2 つの配列を結合するヘルパー メソッド:

var model = [{"bstype":1},{"bstype":2},{"bstype":3},{"bstype":4}];

var modelChildren = [{"bstype":1, "name":"John","Address":"Sample address"}, 
  {"bstype":1, "name":"John","Address":"Sample address"},
  {"bstype":3, "name":"John","Address":"Sample address"},
  {"bstype":2 ,"name":"John","Address":"Sample address"},
  {"bstype":2, "name":"John","Address":"Sample address"},
  {"bstype":4, "name":"John","Address":"Sample address"}];

このメソッドは、一致する bstype で「グループ化」された新しい配列を提供します。

   var result = model.map(function(elem)
          {
              return { 
                  bstype: elem.bstype,
                  children: modelChildren.filter(function(childElem) { 
                      return childElem.bstype == elem.bstype;
                  })
              };
          });
于 2013-03-06T19:59:14.780 に答える
0

私はそれらを 2 つの var 項目に分けました。完了したら、最初の配列を使用して、bstype に基づいて 4 回ループしました。次に、$.root.secondarray を使用して 2 番目の項目をループしました。みんな、ありがとう。

于 2013-03-06T19:45:12.193 に答える