Using a JS Variable Name for a Name Additional Variable

I use node to get a list of our Swimlanes (like an array of JSON objects) for an internal database tool.

Format:

swimlanes = [{"swimlane" : "1"},{"swimlane" : "2"}, ...]

Then I retrieve a list of all instances of MSSQL Server (in the same format).

instances = [{"instance" : "host\instance", "swimlane" : "1"}, ...]

What I want to do is create a JSON object with a name like slInstances, which will be a collection of instance arrays using swimlane.

slInstances = { "1" : ["host\instance", "host\instance"], "2" : ["host\instance", "host\instance"]

I tried:

 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;
                })
            });
        }

(only keeping the "instances" in scope, because I will use it for something else) But that doesn't work at all. My hope was to have sl = something like "1", making my way through each fin. And thus, assign the value for slInstances ["1"] =, slInstances ["2"] = ....

angular - :

<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 ..). , .

?

!

EDIT:

, :

http://plnkr.co/edit/Y2PjAxzsvm201EL7mQC4

+4
3

, :

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.

+2

, forEach:

    scope.swimlanes.forEach(function(swimlane){
    var sl = swimlane;
0

, .

Second instance = [{"instance": "host \ instance", "swimlane": "1"}] contains that swimlane.

Angulars ng-repeat will store every element in the array associated with itself, in this case sl. Thus, sl.swimlane and sl.instance will always be from the same object.

<ul>
  <li ng-repeat="sl in instances ">{{sl.swimlane]}}
    <ul>
        <li>{{sl.instance}}</li>
    </ul>
  </li>
</ul>
0
source

Source: https://habr.com/ru/post/1621566/


All Articles