Trying out Arrays

Page section: playground

So this is using an array in tryout-arrays.js called fruit.

Show

Add items

Remove

Copying

Looping




Array methods

name example effect
push myVar.push(‘squid’) adds squid to the end of the array
unshift myVar.unshift(‘squid’) adds squid to the start of the array
shift myVar.shift() removes the first item
pop myVar.pop() removes the last item
splice myVar.splice(3, 1) removes one item starting with fourth [3]
slice myVar.slice(3, 1) copies one item starting with fourth [3]. Used in a new variable

So with:

let myVar = [[1, "tree"], 2, 3, 4, 5, 6];

and

const mySubVar = ["frog", "Linsang", "stink badger"];



    Looping with a for .. of loop

    Using a for .. of loop through the anythingGoes array.

    agBtn.addEventListener('click', () => {
     ag.innerHTML = '';
     for (let item of anythingGoes) {
       ag.innerHTML += `${item} <br>`;
     }
     ag.innerHTML += `<br>${anythingGoes.join(', ')}`;
    });
    

    Convert to JSON

    Press to convert the JS todos object to JSON format.

    Full list of array methods…

    1. 2:02 reverse
    2. 2:35 push
    3. 3:23 unshift
    4. 3:54 includes
    5. 5:00 isArray
    6. 5:32 from
    7. 6:36 of
    8. 7:01 indexOf
    9. 7:48 lastIndexOf
    10. 8:39 toString
    11. 9:15 toLocaleString
    12. 10:04 join
    13. 10:52 pop
    14. 11:54 shift
    15. 12:25 slice
    16. 13:52 forEach
    17. 15:08 find
    18. 16:18 findIndex
    19. 16:58 some
    20. 18:09 every
    21. 19:07 filter
    22. 20:07 map
    23. 21:32 reduce
    24. 25:29 reduceRight
    25. 26:20 concat
    26. 27:05 fill
    27. 28:44 flat
    28. 30:37 flatMap
    29. 32:50 splice
    30. 34:48 keys
    31. 37:20 values
    32. 37:59 entries
    33. 39:06 sort
    34. 42:25 copyWithin
    View contents of: /static/js/tryout-arrays.js
    const fruit = ["apples", "bananas", "cantaloupe", "dorians"];
    
    const scientificFruit = [
      "Bell pepper",
      " Chile pepper",
      " Corn kernel",
      " Cucumber",
      " Eggplant",
      " JalapeƱo",
      " Olive",
      " Pea",
      " Pumpkin",
      " Squash",
      " Tomato",
      " Zucchini",
    ];
    
    const todos = [
      {
        id: 1,
        text: "Take out rubbish",
        isCompleted: true,
      },
      {
        id: 2,
        text: "Meet with boss",
        isCompleted: true,
      },
      {
        id: 3,
        text: "Dental appointment",
        isCompleted: false,
      },
    ];
    
    function showFruit() {
      document.getElementById("fruitlist").innerHTML = fruit;
    }
    
    function fruitLoops(frt) {
      document.getElementById("fruit-loops").innerHTML += frt + '<br>';
    }
    
    const butt1 = document.getElementById("listfruit");
    
    butt1.addEventListener("click", () => {
      document.getElementById("fruitlist").innerHTML = fruit;
    });
    
    const butt2 = document.getElementById("fruit2");
    
    butt2.addEventListener("click", function () {
      document.getElementById("fruitlist").innerHTML = fruit[1];
    });
    
    const butt3 = document.getElementById("scientific");
    
    butt3.addEventListener("click", function () {
      document.getElementById("fruitlist").innerHTML = scientificFruit;
    });
    
    const butt39 = document.getElementById("copying");
    
    butt39.addEventListener("click", function () {
      const otherFruit = fruit.slice(1, 3);
      console.log("what apphen?");
      alert(`A new array called otherFruit now contains ${otherFruit}`);
    });
    
    // For buttons showing Array Methods
    
    let myVar = [[1, "tree"], 2, 3, 4, 5, 6];
    const mySubVar = ["frog", "Linsang", "stink badger"];
    
    const showMyVar = document.getElementById("show-my-var");
    const smvList = document.getElementById("smv-list");
    
    function listMyVar() {
      smvList.innerHTML = ``;
      for (let i = 0; i < myVar.length; i++) {
        smvList.innerHTML += `<li>${myVar[i]}</li>`;
      }
    }
    
    showMyVar.addEventListener("click", () => {
      listMyVar();
    });
    
    const resetMyVar = document.getElementById("reset-my-var");
    
    resetMyVar.addEventListener("click", () => {
      myVar = [[1, "tree"], 2, 3, 4, 5, 6];
      listMyVar();
      //   alert("reset");
    });