Javascript通过引用或值分配数组元素

时间:2016-11-30 19:49:19

标签: javascript jquery angularjs arrays

有没有办法通过引用而不是按值来明确设置数组元素?

例如,此方法按值设置数组元素:

方法1

var pushes = [2,1]
for(var i=0; i<pushes.length; i++)
{
   vm.allEmployeesOnJob[i] = vm.allEmployees[pushes[i]];                  
}

此方法通过引用设置数组元素:

方法2

var pushes = [2,1]
vm.allEmployeesOnJob = [
                vm.allEmployees[2],
                vm.allEmployees[1]                
              ];

我的用例:

我正在使用angular-bootstrap-duallistbox,为了正确初始化两个列表框,“ng-model”数组必须通过引用引用“ng-options”数组元素。这是由于我读过的解释中“select”DOM元素的行为。我知道第二个是通过引用设置的原因是因为列表框使用该方法正确更新,并且使用第一种方法无法正确更新。

有什么区别?

有没有办法通过引用以编程方式将数组元素显式设置为另一个数组的元素?

修改

vm.allEmployees是一组员工对象。

vm.allEmployees的内容:

[      {  
      "employeeId":1,
      "firstName":"Bill",
      "lastName":"Test",
      "email":null,
      "phoneNumber":null,
      "street":"1234 Sesame",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bill Test - Newaygo"    },    
 {  
      "employeeId":2,
      "firstName":"Bob",
      "lastName":"Test",
      "email":null,
      "phoneNumber":null,
      "street":"1234 Sesame",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bob Test -  Newaygo"    },    
 {  
      "employeeId":4,
      "firstName":"John",
      "lastName":"Doe",
      "email":"testemployee@qas.com",
      "street":"1234 Sesame St",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"John Doe -  Newaygo"    },    
 {  
      "employeeId":5,
      "firstName":"Bill",
      "lastName":"Peterson",
      "email":"bpeterson@test.com",
      "street":"1234 test",
      "city":"test city",
      "state":"Maine",
      "zip":"298379283",
      "activeFlag":false,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bill Peterson -  Newaygo"    },    
 {  
      "employeeId":6,
      "firstName":"Jim",
      "lastName":"Super",
      "email":"jsuper@qas.com",
      "phoneNumber":"459-456-4455",
      "street":"1234 Sesame St",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Jim Super -  Newaygo"    } ]

1 个答案:

答案 0 :(得分:2)

在JavaScript中,对象由引用引用,所有其他值(无论它们是否在数组中)都由value引用。您可以将值存储在对象中,并在需要获得此行为时引用/传递它。

以下两种方法都使用对象并具有“按引用引用”。这两个代码段验证了:

方法1

<html>
    <head>
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script> <!-- here -->
        // Connect to the socket.io server
        var socket = io.connect('http://localhost:8080');
           // ...
        </script>
    </head>
    <body>
        <h1>SSH</h1>
        <div class="terminal"></div>
    </body>
</html>

方法2

var vm = {};

// Here, the contents of the array are ojbects (by reference)
vm.allEmployees = [      {  
      "employeeId":1,
      "firstName":"Bill",
      "lastName":"Test",
      "email":null,
      "phoneNumber":null,
      "street":"1234 Sesame",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bill Test - Newaygo"    },    
 {  
      "employeeId":2,
      "firstName":"Bob",
      "lastName":"Test",
      "email":null,
      "phoneNumber":null,
      "street":"1234 Sesame",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bob Test -  Newaygo"    },    
 {  
      "employeeId":4,
      "firstName":"John",
      "lastName":"Doe",
      "email":"testemployee@qas.com",
      "street":"1234 Sesame St",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"John Doe -  Newaygo"    },    
 {  
      "employeeId":5,
      "firstName":"Bill",
      "lastName":"Peterson",
      "email":"bpeterson@test.com",
      "street":"1234 test",
      "city":"test city",
      "state":"Maine",
      "zip":"298379283",
      "activeFlag":false,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Bill Peterson -  Newaygo"    },    
 {  
      "employeeId":6,
      "firstName":"Jim",
      "lastName":"Super",
      "email":"jsuper@qas.com",
      "phoneNumber":"459-456-4455",
      "street":"1234 Sesame St",
      "city":"New York City",
      "state":"New York",
      "zip":"34555",
      "activeFlag":true,
      "homeLocationId":2,
      "homeLocation":{  
         "locationId":2,
         "customerId":2,
      },
      "displayName":"Jim Super -  Newaygo"    } ]

vm.allEmployeesOnJob = [];

var pushes = [2,1]
for(var i=0; i<pushes.length; i++) {
   vm.allEmployeesOnJob[i] = vm.allEmployees[pushes[i]];                  
}

// The references are by reference:
console.log("Are objects in allEmployeesOnJob the same references as allEmployees: " + (vm.allEmployeesOnJob[0] === vm.allEmployees[2] && vm.allEmployeesOnJob[1] === vm.allEmployees[1]));

// More evidence:
vm.allEmployees[2].employeeId= 999;
console.log("allEmployees[2].employeeId= 999. allEmployeesOnJob[0].employeeId is now: " + vm.allEmployeesOnJob[0].employeeId);