Announcement

Collapse
No announcement yet.

array methode push überschreibt ganzes array?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • array methode push überschreibt ganzes array?

    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

  • #2
    Versuch
    Array.prototype.push.apply(completeList, singleList);

    Warum legst du das Array mit einem prompt an und nicht mit singleList = ["Liste1","Liste2"];
    Christian

    Comment


    • #3
      ich will nicht die elemente des ersten an das zweite array anhängen, ich will das array anhängen,
      ich will mir ein zweidimensionals array bauen das ich mit zwei schleifen durchgehen kann.

      Originally posted by Christian Marquardt View Post
      ...
      Warum legst du das Array mit einem prompt an und nicht mit singleList = ["Liste1","Liste2"];
      ich will am anfang erst eine leere variable weil ich nicht wissen kann was alles dazu kommt,
      ich habs mir in dieser version auch einfach gemacht weil ich rausfinden wollte woran das liegt

      was ich nicht verstehe ist warum das manuell in zwei verschiedenen konsolen geht,
      als script aber nicht
      Zuletzt editiert von pegel; 04.02.2020, 08:27.

      Comment

      Working...
      X