我正在使用node来获取内部数据库工具的Swimlanes列表(作为
JSON对象的数组).
格式:
swimlanes = [{"swimlane" : "1"},{"swimlane" : "2"}, ...]
然后,我检索所有MSSQL Server实例的列表(格式相同).
instances = [{"instance" : "host\instance", "swimlane" : "1"}, ...]
我想要做的是创建一个名为slInstances的JSON对象,它将是泳道实例数组的集合.
slInstances = { "1" : ["host\instance", "host\instance"], "2" : ["host\instance", "host\instance"]
我试过了:
function getInstancesSuccess(response){
$scope.instances = response.data;
$scope.slInstances = {};
$scope.swimlanes.forEach(function(){
var sl = this.swimlane;
$scope.slInstances[sl] = $scope.instances.filter(function(el){
return el.swimlane == sl;
})
});
}
(只保留范围内的“实例”,因为我将把它用于其他东西)但这根本不起作用.我的希望是能够让sl =类似“1”的东西,循环穿过每个泳道.因此为slInstances [“1”] =,slInstances [“2”] = ….分配一个值.
因此,在我的角度模板中,我可以按照以下方式执行以下操作:
<ul>
<li ng-repeat="sl in swimlanes">{{sl['swimlane']}}
<ul>
<li ng-repeat="i in slInstances">{{ i[sl['swimlane']].instance}}
唯一的原因我不是简单地制作一个递增的数组,而是使用这样的对象是因为我们的泳道不是一致的顺序(0,1,22,23,30,31等)所以我希望它是关联的.
我是以完全错误的方式解决这个问题吗?
谢谢!
编辑:
创建了一个以显示:
http://plnkr.co/edit/Y2PjAxzsvm201EL7mQC4
最佳答案 如果我明白你想要完成的事情:
var swimlanes = [{"swimlane" : "1"},{"swimlane" : "2"},{"swimlane" : "3"}];
var instances = [{"instance" : "host1/instance1", "swimlane" : "1"}, {"instance" : "host2/instance2", "swimlane" : "2"}];
var slInstances = {};
for (var i = 0; i < swimlanes.length; i++) {
slInstances[swimlanes[i].swimlane] = [];
}
for (var i = 0; i < instances.length; i++) {
slInstances[instances[i].swimlane].push(instances[i].instance);
}
// returns {"1":["host1/instance1"],"2":["host2/instance2"],"3":[]}
告诉我,如果是这种情况,可以使用underscore.js使其变得更干净.