admin管理员组文章数量:1392099
How could I achieve this below in JavaScript. I tried searching for it on MDN but couldn't find any method for it.
let a, b
let allNumbers = []
for (a = 10; a < 60; a = a + 10) {
for (b = 1; b <= 3; b++) {
allNumbers.push(a + b)
}
}
The desired oute is an array inside the allNumbers
array:
[[11,12,13], [21,22,23], [31,32,33], [41,42,43], [51,52,53]]
How could I achieve this below in JavaScript. I tried searching for it on MDN but couldn't find any method for it.
let a, b
let allNumbers = []
for (a = 10; a < 60; a = a + 10) {
for (b = 1; b <= 3; b++) {
allNumbers.push(a + b)
}
}
The desired oute is an array inside the allNumbers
array:
[[11,12,13], [21,22,23], [31,32,33], [41,42,43], [51,52,53]]
Share
Improve this question
edited Sep 10, 2018 at 12:28
Luca Kiebel
10.1k7 gold badges32 silver badges46 bronze badges
asked Sep 10, 2018 at 12:15
JoerJoer
731 gold badge1 silver badge5 bronze badges
7 Answers
Reset to default 4Just create a temporary Array in the outer loop and push the elements from the inner loop into it, after the inner Loop is finished, push the temporary array in the main one:
let a, b
let allNumbers = []
for (a = 10; a < 60; a += 10) {
let someNumbers = [];
for (b = 1; b <= 3; b++) {
someNumbers.push(a + b)
}
allNumbers.push(someNumbers)
}
console.log(JSON.stringify(allNumbers))
how about this
var a, b
var allNumbers = []
for (a = 10; a < 60; a = a + 10) {
var part = [];
for (b = 1; b <= 3; b++) {
part.push(a + b)
}
allNumbers.push(part)
}
You have to use one second array
.
let a, b
let allNumbers = []
for (a = 10; a < 60; a = a + 10) {
second = [];
for (b = 1; b <= 3; b++) {
second.push(a + b);
}
allNumbers.push(second)
}
console.log(allNumbers);
You can apply a shorted version using ES6
features.
allNumbers = []
for (a = 10; a < 60; a = a + 10) {
allNumbers.push([...Array(3)].map((_, i) => i + a + 1))
}
console.log(allNumbers);
You can try:
const result = Array(5).fill(1).map((a, i) => Array(3).fill(1).map((a, j) => +`${i+1}${j+1}`));
console.log(JSON.stringify(result));
You have to create a new array an add the element to it in the second loop and the add this array to the final one after the second loop.
let a, b
let allNumbers = []
for (a = 10; a < 60; a = a + 10) {
data = []
for (b = 1; b <= 3; b++) {
data.push(a + b)
}
allNumbers.push(data)
}
console.log(allNumbers)
You need to declare a second array inside your loop. Like following:
let a, b
let allNumbers = []
for (a = 10; a < 60; a = a + 10) {
var tempArray = [];
for (b = 1; b <= 3; b++) {
tempArray.push(a + b)
}
allNumbers.push(tempArray);
}
console.log(allNumbers);
Just create an array and push the new array int allNumbers
:
...
let c = []
for (b = 1; b <= 3; b++) {
c.push(a + b)
}
allNumbers.push(c)
...
本文标签: How to add arrays into an array in JavaScriptStack Overflow
版权声明:本文标题:How to add arrays into an array in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775568a2624601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论