简体   繁体   中英

Why can't I push data on the array?

Can you tell me why this code produces error? I want to create a multidimensional array

 const segments = ['avgekcr', 'efgghe', 'ewlskffd']; console.log(segments) let cols = []; let currSegment, currLetter; for (let i = 0; i < segments.length; i += 1) cols.push([]); console.log(cols[2]) //cols.push([]) console.log(cols.length + ' :L') for (let j = 0; j < segments.length; j += 1) { currSegment = segments[j]; for (let k = 0; k < currSegment.length; k += 1) { currLetter = currSegment[k] cols[k].push(currLetter); } }

Error:

Uncaught TypeError: Cannot read property 'push' of undefined

You are pushing with wrong index, corrected the code.

 const segments = ['avgekcr', 'efgghe', 'ewlskffd']; console.log(segments) let cols = []; let currSegment, currLetter; for (let i = 0; i < segments.length; i += 1) cols.push([]); console.log(cols[2]) //cols.push([]) console.log(cols.length + ' :L') for (let j = 0; j < segments.length; j += 1) { currSegment = segments[j]; for (let k = 0; k < currSegment.length; k += 1) { currLetter = currSegment[k] cols[j].push(currLetter); } } console.log(cols)

With the value of k after third iteration, you are trying to push an item in an index which does not really exist.

Change

cols[k].push(currLetter);

To

cols[j].push(currLetter);

 const segments = ['avgekcr', 'efgghe', 'ewlskffd']; console.log(segments) let cols = []; let currSegment, currLetter; for ( let i = 0; i < segments.length; i += 1 ) cols.push([]); console.log(cols[2]) //cols.push([]) console.log(cols.length + ' :L') for ( let j = 0; j < segments.length; j += 1 ) { currSegment = segments[j]; for (let k = 0; k < currSegment.length; k += 1 ) { currLetter = currSegment[k] cols[j].push(currLetter); } } console.log(cols);

You have pushed an empty array to the col only variable 2 times. You are trying to access cols[k] which is from 0 through 7 (if we considered the first index in the segments array).

cols[k] doesn't exist - use cols[j] :

 const segments = ['avgekcr', 'efgghe', 'ewlskffd']; let cols = []; let currSegment, currLetter; for (let i = 0; i < segments.length; i += 1) cols.push([]); for (let j = 0; j < segments.length; j += 1) { currSegment = segments[j]; for (let k = 0; k < currSegment.length; k += 1) { currLetter = currSegment[k] cols[j].push(currLetter); } } console.log(cols);

You can also optimize and make your code much more concise like so:

 const segments = ['avgekcr', 'efgghe', 'ewlskffd']; const cols = segments.map(e => [...e]); console.log(cols);

The above is a lot faster - it has O(N) complexity because you're looping through segments once.

cols仅包含 3 个空数组,并且在某些时候k将大于 2,因此cols[k]返回undefined

I am not concerned about your expected result, just pointing out the error in your code. Take a look at your inner for loop:

for (let k = 0; k < currSegment.length; k += 1) {
        currLetter = currSegment[k]
        cols[k].push(currLetter);

    }

The loop runs on letter 'avgekcr', which has length 7, and length of cols array is just 3. Hence, cols[3] is undefined so it will simply throwing error:

Cannot read property 'push' of undefined

Use the following way to add elements to "cols" because "cols" is one dimensional array. You can use cols[k].push only if that is a multi dimensional array.

cols.push(currSegment[k]);   // no need to define an additional variable to keep currSegment[k] value

And for your implementation simply you can use the split function to make string to a char array and concat function merge the previous cols and new char array.

const segments = ['avgekcr', 'efgghe', 'ewlskffd'];
let cols = [];

for (let i = 0; i < segments.length; i++) {
    cols = cols.concat(segments[i].split(''));
}

Use the following way if you need to access all segments in a 2-dimensional array.

for (let i = 0; i < segments.length; i++) {
    cols.push([]);
    cols[i] = segments[i].split('');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM