最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Array pointers in javascript - Stack Overflow

programmeradmin2浏览0评论

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 es 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.

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 es 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.

Share Improve this question edited Jun 21, 2016 at 22:23 Tomas asked Jun 21, 2016 at 21:41 TomasTomas 711 gold badge1 silver badge4 bronze badges 8
  • No, because it doesn't use pointers like that for memory. (it uses pointers, but not in a way that supports C type math on them) – Nikki9696 Commented Jun 21, 2016 at 21:42
  • 1 that would be nice, but what will happen is it will be converted to a string – Ryan Commented Jun 21, 2016 at 21:43
  • 1 It does support indexes though. Not sure why you don't just use that? Or splice. – Nikki9696 Commented Jun 21, 2016 at 21:43
  • Thank you for the answers. I'll explain my setting and why I need the pointers. I have a huge array (thousands of elements) and I need to feed it into an ng-repeat (I'm using Angular JS). The array contents are to be printed into four columns, hence I need to run ng-repeat four times, once with an ng-if="$index % 4 == 0", once with an ng-if="$index % 4 == 1" and so on. This makes loading too slow. My idea was to have 4 pointers to the appropriate array positions and feed each pointer to the ng-repeat with an limitTo filter. This would go through the array only once instead four times. – Tomas Commented Jun 21, 2016 at 21:57
  • @Tomas Is there a reason why you can't iterate through the list once and just insert the column data into the correct place as you e across the element? – Mike Cluck Commented Jun 21, 2016 at 22:00
 |  Show 3 more ments

3 Answers 3

Reset to default 1

Nope, JavaScript doesn't give you that kind of memory level access.

The best thing you can do (which does create a copy) is abuse slice if you want to pass around parts of the array starting at a given spot. Obviously, this only works if you move forward in the array, not backward.

var arr = [1, 2, 3, 4, 5];

// Kinda, sorta but not really create a pointer at a given point in the array
// It doesn't actually refer to the original array. It creates a copy of a subsection of the array
var pretendPointerButNotAtAll = arr.slice(2);

// Almost like doing pointer arithmetic. You get the same value as doing arr[4]
// or arr + 4 in a language with pointers
var elementAtOffset = pretendPointerButNotAtAll[2];
console.log(elementAtOffset);

Did you want to print out the array from a certain spot to the end in the console?

var arr = [1,2,3,4];
console.log(arr);  
console.log(arr.splice(2));

You can achieve the same effect with

yourArray.splice(2)
发布评论

评论列表(0)

  1. 暂无评论