admin管理员组文章数量:1356092
I'm trying to write a quite simple program that divides an array in another array of defined size smaller arrays, however the push()
method is not working. Could someone please help me with it?
function chunk(array, size) {
var newArray = [];
var tempArray = [];
for (let i = 0; i < array.length / size; i++) {
for (let j = size * i, k = 0; j < size * i + size; j++, k++)
tempArray[k] = array[j];
newArray.push(tempArray);
}
return newArray;
}
var data = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(chunk(data, 2));
The ideal output should be [[1, 2],[3, 4], [5, 6], [7, 8]]
.
However im getting [[7,8],[7,8],[7,8],[7,8]]
.
I'm trying to write a quite simple program that divides an array in another array of defined size smaller arrays, however the push()
method is not working. Could someone please help me with it?
function chunk(array, size) {
var newArray = [];
var tempArray = [];
for (let i = 0; i < array.length / size; i++) {
for (let j = size * i, k = 0; j < size * i + size; j++, k++)
tempArray[k] = array[j];
newArray.push(tempArray);
}
return newArray;
}
var data = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(chunk(data, 2));
The ideal output should be [[1, 2],[3, 4], [5, 6], [7, 8]]
.
However im getting [[7,8],[7,8],[7,8],[7,8]]
.
-
2
move
var tempArray = [];
inside of your first for loop. Currently, you're pushing the same array reference each time, and so modifications to it will impact it within your array – Nick Parsons Commented Aug 3, 2021 at 6:21 -
1
You are creating only one
temparray
in he very beginning and then overwriting its elements and pushing the same array multiple times – derpirscher Commented Aug 3, 2021 at 6:22
3 Answers
Reset to default 6You're almost there. Just move the tempArray
definition inside your first for
-loop. Otherwise you would be pushing the same array each time.
Working Example:
function chunk(array, size) {
const newArray = [];
for (let i = 0; i < array.length / size; i++) {
const tempArray = [];
for (let j = size * i, k = 0; j < size * i + size; j++, k++)
tempArray[k] = array[j];
newArray.push(tempArray);
}
return newArray;
};
const data = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(chunk(data, 2)); // [[1, 2],[3, 4], [5, 6], [7, 8]]
@Behemoth's answer is the correct one for the question. But if you want, you can take a slightly different approach like this to reach the solution as well.
function chunk(array, size){
const newArray = [];
let i,j;
for (i = 0,j = array.length; i < j; i += size) {
newArray.push(array.slice(i, i + size));
}
return newArray;
};
var data = [1, 2, 3, 4, 5, 6, 7 ,8];
console.log(chunk(data, 2));
Slightly different solution than @Behemoth and @Rukshan
function chunk(array, size) {
var newArray = [];
var tempArray = [];
for (let i = 0; i < array.length / size; i++) {
for (let j = i; j < i + 1; j++) {
for (let k = 0; k < size; k++) {
if (array[size * j + k]) {
tempArray.push(array[size * j + k]);
}
}
newArray.push(tempArray);
tempArray = [];
}
}
return newArray;
};
var data = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(chunk(data, 2));
The solution would be [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9 ] ] instead of [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ]]
本文标签: push() method not working properly in JavaScriptStack Overflow
版权声明:本文标题:push() method not working properly in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743952507a2567547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论