Does JavaScript support pointer arithmetic in some way?
For example, I have the array
var arr = [1,2,3,4];
console.log(arr); /* this prints [1,2,3,4] */
Is it possible to do something like:
var arr2 = arr + 2; /* obviously this is not the correct way */
console.log(arr2); /* this should have printed [3,4] */
without creating a copy of the array (I only need a pointer as in C).
The specific reason I need this is the following: I have a large array where each element is a pair of string,boolean, e.g.
var arr = [['name1',true],['name2',false]];
I want to feed the array into an ng-repeat (I'm using Angular JS). The array consists of several thousands of such elements, and comes from a service/factory (i.e returned by reference). I want to display the contents of the array in 4 columns, with a checkbox after each string. To do that, I have 4 ng-repeats in the following way:
<div class="col-lg-3">
<div class="checkbox" ng-repeat="x in arr" ng-if="$index % 4 == 0">
<label><input type="checkbox" ng-model="x[1]">{{x[0]}}</label>
</div>
</div>
<div class="col-lg-3">
<div class="checkbox" ng-repeat="x in arr" ng-if="$index % 4 == 1">
<label><input type="checkbox" ng-model="x[1]">{{x[0]}}</label>
</div>
</div>
<div class="col-lg-3">
<div class="checkbox" ng-repeat="x in arr" ng-if="$index % 4 == 2">
<label><input type="checkbox" ng-model="x[1]">{{x[0]}}</label>
</div>
</div>
<div class="col-lg-3">
<div class="checkbox" ng-repeat="x in arr" ng-if="$index % 4 == 3">
<label><input type="checkbox" ng-model="x[1]">{{x[0]}}</label>
</div>
</div>
This method is very slow because I need to traverse the array 4 times. I cannot pre-process the array (i.e., split it into four equally large parts that I would pass to each ng-repeat without the ng-if) because I use the ng-model directive to allow the user to update the array, and in turn the service (this is done automatically since the array was returned by reference from the service).
Hence, the (theoretically) ideal would be to have four pointers (each pointing to a specific part of the array) and pass each pointer to the ng-repeat with a limitTo filter.