1

I am trying to define this array:

    postLists = [
    [
        [46276,76235,78128],
        postLists[0][0].length,
        1100
    ], [
        [],
        postLists[1][0].length,
        0
    ]
];

However, I get an undefined error for all the postLists[x][0].length lines.

How do I access the array being defined from itself? Is there a way to relatively select an item without referencing its entire "path" like you would do with folders? For example in this case, something like [0].length would get [46276,76235,78128]'s length, or ..[1] (parent) would select postLists[1].

postLists[x][0] will hold hundreds of thousands of integers, so performance need to be considered.
postLists[x][1] is the original length which needs to be accessed every few seconds, because of the size of postLists[x][0], it cannot be accessed on the fly so often without harming performance.
postLists[x][2] is an index to keep track (and store) the items processed, in postLists[0][2] the 1100 is used to skip the 1100 first items which have already been processed.

I am using this in a Greasemonkey script, the reason for the sub-arrays in postLists is that I plan to use the script on multiple tabs running at the same time.

The start of the script is setup like this:

window.onkeydown = function(event) {
    if (event.ctrlKey && event.altKey) {
        switch(event.keyCode) {
            case 49: activeList = postLists[0]; break; // 1
            case 50: activeList = postLists[1]; break; // 2
            case 51: activeList = postLists[2]; break; // 3
            case 52: activeList = postLists[3]; break; // 4

            case 70: // F
                toggleScript = !toggleScript;
                if (toggleScript) {
                    treatItem();
                    favObserver.observe(topNotice, {attributes: false, childList: true, characterData: true});
                } else
                    stopObserving(true);
                break;
        }
    }
};

Every functions then use activeList to refer the selected sub-array.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
an01
  • 15
  • 5
  • 2
    You can't do that in an array literal, since the value hasn't been assigned to *postLists* yet, so *postLists[0]* doesn't exist yet (unless it's been assigned previously). – RobG Aug 29 '16 at 02:54
  • 1
    This is similar to [other questions about object literals](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations), though I don't recall seeing one specifically about an array before. Same principle though. (Why do you need the array lengths as separate elements though? Can't you just use `postLists[x][0].length` to get the current length of that array whenever you need it?) – nnnnnn Aug 29 '16 at 02:58
  • @an01 why don't you poputale the lengths *after* setting the array? – Gerardo Furtado Aug 29 '16 at 02:59
  • 1
    P.S. You can do something like this: `postLists = [ [ [46276,76235,78128], 0, 1100 ], [ [], 0, 0 ] ].map(v => { v[1]=v[0].length; return v });` – nnnnnn Aug 29 '16 at 03:03
  • @GerardoFurtado For various reasons in my program, I need the length item to be in second. The array will handle up to hundreds of thousands of variables, so I'd rather avoid using something like splice() which would probably be a huge slowdown – an01 Aug 29 '16 at 03:03

1 Answers1

2

You can utilize assignment, with side-effect of creating additional variable; e.g., tmp, used to assign, re-assign a variable which can change multiple occassions

var tmp, postLists = [
  [
    tmp = [46276, 76235, 78128],
    tmp.length,
    1100
  ],
  [
    tmp = [],
    tmp.length,
    0
  ]
];
delete tmp;
console.log(postLists);
guest271314
  • 1
  • 15
  • 104
  • 177
  • @nnnnnn Yes, you are correct. See updated post, adjusted approach to use single variable which changes, or can be changed multiple occasions – guest271314 Aug 29 '16 at 03:11
  • 1
    Yep, saw that. +1 on the edited version. (You could also declare `var tmp, postLists = ...` to avoid a global.) – nnnnnn Aug 29 '16 at 03:12