Skip to main content
dublove
Legend
Nov 26, 2026 5:20 PM UTC
Answered

How to replace listing five conditions separately with a loop?

I originally wrote it this way, listing them one by one.

The actual situation is much more complex, let me give a simple example:


var af0 = [];
var af1 = [];
var af2 = [];
var af3 = [];
for (var i = 0; i < ps.length; i++) {
if (1 < a) {
bf[1].push(ps[i]);
}
if (2 < a) {
bf[2].push(ps[i]);
}
if (2 < a) {
bf[2].push(ps[i]);
}
}

I think it's too complicated and I want to change it like this, but I don't recognize bf [y]

var af0 = [];
var af1 = [];
var af2 = [];
var af3 = [];
for (var i = 0; i < ps.length; i++) {
for (var y = 1; y <= 3; y++) {
if (y < a) {
bf[y].push(ps[i]);
}
}
}

I even want to use nested loops to read the values in each bf, but bf [y] [i] can't recognize them anymore.

            for (var y = 0; y <= 5; y++) {
for (var i = 0; i < bf[y].length; i++) {
bf[y][i].leading= oldLeading* y;
}

 

    CORCTON
    CORCTONCorrect answer
    Community Expert
    Nov 26, 2026 6:01 PM UTC

    Hi dublove — the problem is that af0, af1, af2 and af3 are four separate variables, so JavaScript has no array called bf for bf[y] to refer to. You can't build a variable name from a number like that. Instead, put the four arrays inside one array:

     

    var bf = [[], [], [], []];

     

    for (var i = 0; i < ps.length; i++) {

        for (var y = 1; y <= 3; y++) {

            if (y < a) {

                bf[y].push(ps[i]);

            }

        }

    }

     

    Then bf[y] is a real array, and your nested loop works — but fix its limit: you only have 4 buckets (indexes 0-3), so y <= 5 runs past the end and bf[4] / bf[5] are undefined. Use y < bf.length:

     

    for (var y = 0; y < bf.length; y++) {

        for (var i = 0; i < bf[y].length; i++) {

            bf[y][i].leading = oldLeading * y;

        }

    }

     

    One more note: in your first snippet you declare af0-af3 but push to bf[1] — make sure the names match (use the single bf array above for both parts).

    2 replies

    CORCTON
    Community Expert
    CORCTONCommunity ExpertCorrect answer
    Community Expert
    Nov 26, 2026 6:01 PM UTC

    Hi dublove — the problem is that af0, af1, af2 and af3 are four separate variables, so JavaScript has no array called bf for bf[y] to refer to. You can't build a variable name from a number like that. Instead, put the four arrays inside one array:

     

    var bf = [[], [], [], []];

     

    for (var i = 0; i < ps.length; i++) {

        for (var y = 1; y <= 3; y++) {

            if (y < a) {

                bf[y].push(ps[i]);

            }

        }

    }

     

    Then bf[y] is a real array, and your nested loop works — but fix its limit: you only have 4 buckets (indexes 0-3), so y <= 5 runs past the end and bf[4] / bf[5] are undefined. Use y < bf.length:

     

    for (var y = 0; y < bf.length; y++) {

        for (var i = 0; i < bf[y].length; i++) {

            bf[y][i].leading = oldLeading * y;

        }

    }

     

    One more note: in your first snippet you declare af0-af3 but push to bf[1] — make sure the names match (use the single bf array above for both parts).

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 27, 2026 8:27 AM UTC

    I succeeded, thank you

    Translate