[英]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.使用第三次迭代后的
k
值,您试图将一个项目推送到一个实际上并不存在的索引中。
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.您已将空数组推送到 col only 变量 2 次。 You are trying to access cols[k] which is from 0 through 7 (if we considered the first index in the segments array).
您正在尝试访问从 0 到 7 的 cols[k](如果我们考虑了 segment 数组中的第一个索引)。
cols[k]
doesn't exist - use cols[j]
: cols[k]
不存在 - 使用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.上面的速度要快得多 - 它具有
O(N)
复杂度,因为您要循环一次segments
。
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 循环:
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:循环在字母 'avgekcr' 上运行,它的长度为 7,而cols数组的长度仅为 3。因此, cols[3] 未定义,因此它只会抛出错误:
Cannot read property 'push' of undefined
Use the following way to add elements to "cols" because "cols" is one dimensional array.使用以下方式向“cols”添加元素,因为“cols”是一维数组。 You can use cols[k].push only if that is a multi dimensional array.
只有当它是一个多维数组时,您才能使用 cols[k].push。
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.对于您的实现,您可以使用 split 函数将字符串转换为字符数组,并使用 concat 函数合并以前的列和新的字符数组。
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('');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.