简体   繁体   English

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

[英]Javascript assign array elements by reference or by value

Is there a way to explicity set array elements by reference instead of by value? 有没有办法通过引用而不是通过值显式设置数组元素?

For example, this method sets the array elements by value: 例如,此方法通过值设置数组元素:

METHOD 1 方法1

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

And this method sets the array elements by reference: 并且此方法通过引用设置数组元素:

METHOD 2 方法2

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

My use case: 我的用例:

I am using angular-bootstrap-duallistbox and in order to initialize the two listboxes correctly, the "ng-model" array must reference the "ng-options" array elements by reference. 我正在使用angular-bootstrap-duallistbox,为了正确初始化两个列表框,“ ng-model”数组必须通过引用引用“ ng-options”数组元素。 This is due to the behavior of the "select" DOM element from explanations I have read. 这是由于我已阅读的说明中的“ select” DOM元素的行为所致。 The reason I know that the second is setting by reference is because the listbox updates properly using that method and it does not update properly using the first method. 我知道第二个是通过引用设置的原因是因为列表框使用该方法可以正确更新,而使用第一个方法则不能正确更新。

What could the difference be? 有什么区别?

And is there a way to programmatically set array elements to another array's elements explicitly by reference? 有没有一种方法可以通过编程将数组元素以编程方式设置为另一个数组的元素?

EDIT: 编辑:

vm.allEmployees is an array of employee objects. vm.allEmployees是一组雇员对象。

Content of 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"    } ]

In JavaScript objects are referenced by reference and all other values (regardless of whether they are in an array or not) are referenced by value. 在JavaScript中,对象是通过引用引用的,而所有其他值(无论它们是否在数组中)都是通过值引用的。 You can store your value(s) in an object and refer/pass that if you need to get this behavior. 您可以将值存储在对象中,如果需要获得此行为,则可以引用/传递该值。

Both of your methods below are working with objects and have By Reference references. 您下面的两种方法都可以使用对象,并且具有“按引用”引用。 These two snippets verify that: 这两个摘要验证:

METHOD 1 方法1

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

METHOD 2 方法2

 var vm = {}; 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" } ] var pushes = [2,1] vm.allEmployeesOnJob = [ vm.allEmployees[2], vm.allEmployees[1] ]; console.log("Are objects the same (by ref)? " + (vm.allEmployeesOnJob[0] === vm.allEmployees[2] && vm.allEmployeesOnJob[1] === vm.allEmployees[1])); // Further evidence: vm.allEmployees[2].employeeId = 999; console.log("allEmployees[2].employeeId was changed to 999. What is allEmployeesOnJob[0].employeeId now? " + vm.allEmployeesOnJob[0].employeeId); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM