0

So, here, I am trying to create a two dimensional array; an array of arrays. However, whenever I try to push a value to the nested array, I receive the error seen in the title. In these for loops, the 'b' array is full of a list of strings, which are all received in a certain format, and the code then breaks it up and pushes it to the values in the 2-D array for further working.

var c = []

for(var u = 0; u < b.length; u++){
  c[u] = new Array(0);
  for(var y = 0; y < b[u].length; y++){
    c[u][y].push(b[u].substring(b[u].indexOf('\"'), b[u].indexOf(",")));
    b[u] = b[u].substring(b[u].indexOf(',') + 1);
  }
}

I've looked on the other solutions here on stackoverflow; Either I can't wrap my head around them, or they're not applicable here. I can't tell what's wrong here. Help, please.

6
  • Well for one thing, when the process starts, b.length is zero; at the start there's nothing in the arrays yet, so the outer loop will perform no iterations. Commented May 25, 2016 at 14:05
  • b already contains strings, filled by another loop. I'll clarify, sorry. Commented May 25, 2016 at 14:06
  • What are you trying to do? I'm almost sure there's a better way than a nested for loop and stateful calls to .push(). Commented May 25, 2016 at 14:08
  • 1
    It's hard to tell what sort of results you want, but it probably should just be c[u].push(...) instead of c[u][y].push(...). Commented May 25, 2016 at 14:09
  • It's interpreting an input in a list format, separated by commas. For some reason, where I'm getting it from insists on using a '"' around each element in the list. I'm going to be doing other things to these strings afterwards, though, I'm just trying to mince it into chunks. Commented May 25, 2016 at 14:10

3 Answers 3

2
var c = []

for(var u = 0; u < b.length; u++){
  c[u] = new Array(0);
  for(var y = 0; y < b[u].length; y++){
    // NOT WORKING
    // c[u][y].push(b[u].substring(b[u].indexOf('\"'), b[u].indexOf(",")));

    // WORKING
    c[u].push(b[u].substring(b[u].indexOf('\"'), b[u].indexOf(",")));

    // WORKING
    c[u][y] = b[u].substring(b[u].indexOf('\"'), b[u].indexOf(","));

    b[u] = b[u].substring(b[u].indexOf(',') + 1);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

glad to know it helps.
0

As you already declare an array b as blank without any value var b = [].

So, b.length will be 0.

Then this condition for(var u = 0; u < b.length; u++) will not execute and code will not work.

Update :

So, you can directly use :

c[u].push(b[u].substring(b[u].indexOf('\"'), b[u].indexOf(",")));

1 Comment

b contains strings. Earlier, var b = [] was used as a placeholder for this question only. Apologies for the confusion.
0

I think the problem here is that you are trying to use .push on something that is already a value:

/**
 * c is an array: []
 */
var c = []

for (var u = 0; u < b.length; u++) {
  /**
   * c[u] is a subarray: [u: []]
   */
  c[u] = new Array(0);
  for (var y = 0; y < b[u].length; y++) {
    /**
     * c[u][y] is a value, currently undefined: [u: [y: ??]]
     */
    c[u][y].push(b[u].substring(b[u].indexOf('\"'), b[u].indexOf(",")));
    b[u] = b[u].substring(b[u].indexOf(',') + 1);
  }
}

For what you want, you use either of the following:

// Value (replace as needed)
var value = b[u].substring(b[u].indexOf('\"'), b[u].indexOf(","));

// Option 1
c[u][y] = value;

// Option 2
c[u].push(value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.