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

一个帮助方法来组合两个数组,就像你用同一个键完成的那样:

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

我将它们分成两个 var 项,第一个包含第一个数组,第二个数组。完成后,我使用第一个数组根据 bstype 循环 4 次。然后使用 $.root.secondarray 遍历第二项。谢谢大家。

于 2013-03-06T19:45:12.193 回答