Javascript: Turn a nested array into a single array using a nested for loop
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 |
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 |
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 '18 at 16:19
simonsimon
31
31
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
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 |
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 |
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 |
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
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 |
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 |
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 16:26
brkbrk
27.9k32142
27.9k32142
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:23
bipllbipll
8,2361927
8,2361927
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:25
GeorgeGeorge
4,50811732
4,50811732
add a comment |
add a comment |
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
Array.concat can do this on its own
var merged = .concat.apply(, array);
add a comment |
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 '18 at 16:26
SpeedOfRoundSpeedOfRound
629314
629314
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:28
Ayon SahaAyon Saha
30427
30427
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:24
ChristheoreoChristheoreo
2269
2269
add a comment |
add a comment |
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
add a comment |
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 '18 at 16:25
weibenfalkweibenfalk
54617
54617
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:32
bugpulverbugpulver
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.
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