Javascript: Turn a nested array into a single array using a nested for loop
up vote
0
down vote
favorite
This is just a simple javascript exercise that I'm working on.
I'm trying to convert this array...
var array = [
[1,2],
[3,4],
[5,6]
];
into...
array = [1, 2, 3, 4, 5, 6];
by using this nested for loop.
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};
console.log(storage);
With an output of...
//Output: [6, 6, 6, 6, 6, 6]
Why is this the output and how can I fix it?
javascript loops for-loop nested
add a comment |
up vote
0
down vote
favorite
This is just a simple javascript exercise that I'm working on.
I'm trying to convert this array...
var array = [
[1,2],
[3,4],
[5,6]
];
into...
array = [1, 2, 3, 4, 5, 6];
by using this nested for loop.
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};
console.log(storage);
With an output of...
//Output: [6, 6, 6, 6, 6, 6]
Why is this the output and how can I fix it?
javascript loops for-loop nested
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is just a simple javascript exercise that I'm working on.
I'm trying to convert this array...
var array = [
[1,2],
[3,4],
[5,6]
];
into...
array = [1, 2, 3, 4, 5, 6];
by using this nested for loop.
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};
console.log(storage);
With an output of...
//Output: [6, 6, 6, 6, 6, 6]
Why is this the output and how can I fix it?
javascript loops for-loop nested
This is just a simple javascript exercise that I'm working on.
I'm trying to convert this array...
var array = [
[1,2],
[3,4],
[5,6]
];
into...
array = [1, 2, 3, 4, 5, 6];
by using this nested for loop.
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};
console.log(storage);
With an output of...
//Output: [6, 6, 6, 6, 6, 6]
Why is this the output and how can I fix it?
javascript loops for-loop nested
javascript loops for-loop nested
asked Nov 22 at 16:19
simon
31
31
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
up vote
2
down vote
accepted
for (var k = 0; k < 6; k++) {
is not required . array[i]
will be each of element inside main array , so iterating over array[i]
you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
add a comment |
up vote
2
down vote
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
add a comment |
up vote
1
down vote
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series
, which in your case would be 6
add a comment |
up vote
1
down vote
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
add a comment |
up vote
0
down vote
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
add a comment |
up vote
0
down vote
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
up vote
0
down vote
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
up vote
0
down vote
storage = ;
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
add a comment |
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
for (var k = 0; k < 6; k++) {
is not required . array[i]
will be each of element inside main array , so iterating over array[i]
you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
add a comment |
up vote
2
down vote
accepted
for (var k = 0; k < 6; k++) {
is not required . array[i]
will be each of element inside main array , so iterating over array[i]
you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
for (var k = 0; k < 6; k++) {
is not required . array[i]
will be each of element inside main array , so iterating over array[i]
you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
for (var k = 0; k < 6; k++) {
is not required . array[i]
will be each of element inside main array , so iterating over array[i]
you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
answered Nov 22 at 16:26
brk
25.2k31939
25.2k31939
add a comment |
add a comment |
up vote
2
down vote
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
add a comment |
up vote
2
down vote
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
add a comment |
up vote
2
down vote
up vote
2
down vote
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
answered Nov 22 at 16:23
bipll
7,8991825
7,8991825
add a comment |
add a comment |
up vote
1
down vote
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series
, which in your case would be 6
add a comment |
up vote
1
down vote
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series
, which in your case would be 6
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series
, which in your case would be 6
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series
, which in your case would be 6
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), ));
answered Nov 22 at 16:25
George
4,38711731
4,38711731
add a comment |
add a comment |
up vote
1
down vote
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
add a comment |
up vote
1
down vote
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
add a comment |
up vote
1
down vote
up vote
1
down vote
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = ;
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
answered Nov 22 at 16:28
Ayon Saha
30417
30417
add a comment |
add a comment |
up vote
0
down vote
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
add a comment |
up vote
0
down vote
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
answered Nov 22 at 16:24
Christheoreo
2218
2218
add a comment |
add a comment |
up vote
0
down vote
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
up vote
0
down vote
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
answered Nov 22 at 16:25
weibenfalk
50115
50115
add a comment |
add a comment |
up vote
0
down vote
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
up vote
0
down vote
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
up vote
0
down vote
up vote
0
down vote
Array.concat can do this on its own
var merged = .concat.apply(, array);
Array.concat can do this on its own
var merged = .concat.apply(, array);
answered Nov 22 at 16:26
SpeedOfRound
603314
603314
add a comment |
add a comment |
up vote
0
down vote
storage = ;
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
add a comment |
up vote
0
down vote
storage = ;
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
add a comment |
up vote
0
down vote
up vote
0
down vote
storage = ;
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
storage = ;
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
answered Nov 22 at 16:32
bugpulver
85
85
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434913%2fjavascript-turn-a-nested-array-into-a-single-array-using-a-nested-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown