0

I am practicing Javascript and have the following code which returns an Uncaught TypeError: Cannot set property '0' of undefined error on the sets[i][j] = initial_sets [i][j]; line. The idea seems to be correct, but I can't figure out why I am getting the error.

var sets = [[],[]]; //Declared this 2D array based on solution in https://stackoverflow.com/questions/16512182/how-to-create-empty-2d-array-in-javascript
var initial_sets=[[a,b],[c,d],[e,f]]; //2D array
var i,j;

//2D array sets is being filled by 2D array initial_sets
for (i=0; i<initial_sets.length; i++) {
    for (j=0; j<initial_sets[i].length; j++) {
            sets[i][j] = initial_sets[i][j];
    }
}

I tried the solution posted in Uncaught TypeError: Cannot set property '0' of undefined " by creating a 1D array and then assigning another dimension to it, but I still get the same error.

5
  • You have 3 arrays in initial_sets but only 2 in sets so set[2] is undefined Commented Jan 15, 2017 at 16:49
  • initial_sets is a 2D array though Commented Jan 15, 2017 at 16:50
  • What is the end goal of the code? Commented Jan 15, 2017 at 16:54
  • [[],[]]; here size of array is 2, so how can you fit anything at 3rd index Commented Jan 15, 2017 at 16:57
  • based on this solution stackoverflow.com/questions/16512182/… it is a declaration for a 2D array. not an array with only 2 indices Commented Jan 15, 2017 at 17:11

3 Answers 3

2
var sets = [];
var initial_sets=[[a,b],[c,d],[e,f]];
var i,j;

for (i=0; i<initial_sets.length; i++) {
    // check if sets[i] exist. if not push a new emty array to it.
    if(!sets[i]) sets.push([]);
    for (j=0; j<initial_sets[i].length; j++) {
            sets[i][j] = initial_sets[i][j];
    }
}

then you won't have to initialize sets with empty arrays, it will push them when needed.

Sign up to request clarification or add additional context in comments.

Comments

0

Dimensions of the arrays are not the same.

var sets = []; 
var initial_sets=[["a","b"],["c","d"],["e","f"]];
var i,j;

for (i=0; i<initial_sets.length; i++) { 
    var tmp = [];
    for (j=0; j<initial_sets[i].length; j++) {
            tmp[j] = initial_sets[i][j];
    }
    sets[i] = tmp;
}

OR

var sets = []; 
var initial_sets=[["a","b"],["c","d"],["e","f"]];
var i,j;

for (i=0; i<initial_sets.length; i++) { 
    sets[i] = []; 
    for (j=0; j<initial_sets[i].length; j++) {
            sets[i][j] = initial_sets[i][j];
    }    
}

5 Comments

yes, they are.. they are both 2D. And sets will dynamically increase to the size of initial_sets.
@user2883071 When i==2, sets[i] is undefined. It's true dynamically increases but the value of sets[2] is undefined. So sets[2][j] is undefined and can not be set
Apologies, I see what you are saying. I believe my confusion arose when I tried to declare a 2D array. I tried initializing a 2D array as such: var sets = [][]; but that gave me an error: Uncaught SyntaxError: Unexpected token ], which is why I used var sets = [[],[]];
It was based on the following solution: stackoverflow.com/questions/16512182/…
@user2883071 You defined [[], []] correctly. And you know access array with indices causes the array length to be increased. But the point is when you want to access to sets[i][j] and i==2, the sets[i] does not get valued yet and is undefined. So before accessing to sets[2][j] you need to set value for sets[2]
0

Try this:

var sets = [];
var initial_sets=[['a','b'],['c','d'],['e','f']];
var i,j;

for (i=0; i<initial_sets.length; i++){ 
  sets[i]=[];
  for (j=0; j<initial_sets[i].length; j++){
    sets[i].push(initial_sets[i][j]);
  }
}
console.log(sets);

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.