hi leute,
habe seit ein paar tagen ein kleines problem das mir etwas merkwürdig vorkummt:
ich möchte ein array mit mehrere anderen array füllen,
immer wenn ich mit der methode push, oder auch mit einer veriable als index des arrays
etwas anhängen will, wird mir das komplette array überschrieben,
hier mal der code und die konsolenausgabe in firefox:
arrayTest.js
var counter = 0;
var singleList = [];
var completeList = [];
function log(){
console.log(counter);
console.log(singleList);
console.log(completeList);
}
function userInput(){
singleList[0] = prompt("Liste 1: ");
singleList[1] = prompt("Liste 2: ");
singleList[2] = prompt("Liste 3: ");
}
log();
function arrayTest(){
userInput();
/*completeList[counter] = singleList;*/
completeList.push(singleList);
log();
counter++;
}
arrayTest();
document.getElementById('one').innerHTML = completeList;
arrayTest();
document.getElementById('two').innerHTML = completeList;
arrayTest();
document.getElementById('three').innerHTML = completeList;
hier die konsolenausgabe:
0 []
length: 0
<prototype>: Array []
[] length: 0
<prototype>: Array []
0 (3) […]
0: "1"
1: "2"
2: "3"
length: 3
<prototype>: Array []
(1) […]
0: Array(3) [ "1", "2", "3" ]
length: 1 <prototype>: Array []
1 (3) […]
0: "4"
1: "5"
2: "6"
length: 3
<prototype>: Array []
(2) […]
0: Array(3) [ "4", "5", "6" ]
1: Array(3) [ "4", "5", "6" ]
length: 2
<prototype>: Array []
2 (3) […]
0: "7"
1: "8"
2: "9"
length: 3
<prototype>: Array []
(3) […] 0: Array(3) [ "7", "8", "9" ]
1: Array(3) [ "7", "8", "9" ]
2: Array(3) [ "7", "8", "9" ]
length: 3
<prototype>: Array []
wenn ich das ganze manuell in die browserkonsole oder in node.js eingebe wird immer
der wert der aktuellen veriable angehängt und die werte vorher bleiben wie sie sind,
kann mir das jemand erklären?
grüße
habe seit ein paar tagen ein kleines problem das mir etwas merkwürdig vorkummt:
ich möchte ein array mit mehrere anderen array füllen,
immer wenn ich mit der methode push, oder auch mit einer veriable als index des arrays
etwas anhängen will, wird mir das komplette array überschrieben,
hier mal der code und die konsolenausgabe in firefox:
arrayTest.js
var counter = 0;
var singleList = [];
var completeList = [];
function log(){
console.log(counter);
console.log(singleList);
console.log(completeList);
}
function userInput(){
singleList[0] = prompt("Liste 1: ");
singleList[1] = prompt("Liste 2: ");
singleList[2] = prompt("Liste 3: ");
}
log();
function arrayTest(){
userInput();
/*completeList[counter] = singleList;*/
completeList.push(singleList);
log();
counter++;
}
arrayTest();
document.getElementById('one').innerHTML = completeList;
arrayTest();
document.getElementById('two').innerHTML = completeList;
arrayTest();
document.getElementById('three').innerHTML = completeList;
hier die konsolenausgabe:
0 []
length: 0
<prototype>: Array []
[] length: 0
<prototype>: Array []
0 (3) […]
0: "1"
1: "2"
2: "3"
length: 3
<prototype>: Array []
(1) […]
0: Array(3) [ "1", "2", "3" ]
length: 1 <prototype>: Array []
1 (3) […]
0: "4"
1: "5"
2: "6"
length: 3
<prototype>: Array []
(2) […]
0: Array(3) [ "4", "5", "6" ]
1: Array(3) [ "4", "5", "6" ]
length: 2
<prototype>: Array []
2 (3) […]
0: "7"
1: "8"
2: "9"
length: 3
<prototype>: Array []
(3) […] 0: Array(3) [ "7", "8", "9" ]
1: Array(3) [ "7", "8", "9" ]
2: Array(3) [ "7", "8", "9" ]
length: 3
<prototype>: Array []
wenn ich das ganze manuell in die browserkonsole oder in node.js eingebe wird immer
der wert der aktuellen veriable angehängt und die werte vorher bleiben wie sie sind,
kann mir das jemand erklären?
grüße
Comment