How best to do a JavaScript array with non-consecutive indexes?

前端 未结 7 889
野性不改
野性不改 2020-12-06 00:09

I\'m writing a Google Chrome extension, in JavaScript, and I want to use an array to store a bunch of objects, but I want the indexes to be specific non-consecutive

相关标签:
7条回答
  • 2020-12-06 01:00

    You could attempt to do something like this to make it loud and clear to the JIST compiler that this is a more objecty-ish array like so:

        window.SparseArray = function(len){
          if (typeof len === 'number')
            if (0 <= len && len <= (-1>>>0))
              this.length = len;
            else
              new Array(len); // throws an Invalid array length type error
          else
            this.push.apply(this, arguments);
        }
        window.SparseArray.prototype = Array.prototype
    
    0 讨论(0)
提交回复
热议问题