Manipulating Arrays
12345678910111213141516171819// Adding items to an array const fruits = ["apple", "banana"]; fruits.push("orange"); // Adds "orange" to the end fruits.unshift("strawberry"); // Adds "strawberry" to the beginning // Removing items from an array const lastFruit = fruits.pop(); // Removes the last item ("orange") const firstFruit = fruits.shift(); // Removes the first item ("strawberry") // Iterating over array elements fruits.forEach(function(fruit, index) { console.log(index + ": " + fruit); }); // Creating a new array with map const upperFruits = fruits.map(function(fruit) { return fruit.toUpperCase(); }); console.log(upperFruits); // ["APPLE", "BANANA"]
Arrays in JavaScript are flexible, allowing you to add, remove, and transform elements efficiently. The push method adds one or more elements to the end of an array, while pop removes the last element and returns it. If you want to add or remove elements at the beginning, use unshift and shift respectively. These methods make it easy to manage lists of data, such as keeping track of items in a shopping cart.
To process or examine each item in an array, use the forEach method. This runs a function for every element, giving you access to both the value and its index. When you need to create a new array based on the original, such as converting all strings to uppercase, use the map method. map applies a function to each element and returns a new array with the results, without changing the original array.
These array methods help you build dynamic, interactive experiences by letting you easily update, display, and transform lists of data.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 6.25
Manipulating Arrays
Veeg om het menu te tonen
12345678910111213141516171819// Adding items to an array const fruits = ["apple", "banana"]; fruits.push("orange"); // Adds "orange" to the end fruits.unshift("strawberry"); // Adds "strawberry" to the beginning // Removing items from an array const lastFruit = fruits.pop(); // Removes the last item ("orange") const firstFruit = fruits.shift(); // Removes the first item ("strawberry") // Iterating over array elements fruits.forEach(function(fruit, index) { console.log(index + ": " + fruit); }); // Creating a new array with map const upperFruits = fruits.map(function(fruit) { return fruit.toUpperCase(); }); console.log(upperFruits); // ["APPLE", "BANANA"]
Arrays in JavaScript are flexible, allowing you to add, remove, and transform elements efficiently. The push method adds one or more elements to the end of an array, while pop removes the last element and returns it. If you want to add or remove elements at the beginning, use unshift and shift respectively. These methods make it easy to manage lists of data, such as keeping track of items in a shopping cart.
To process or examine each item in an array, use the forEach method. This runs a function for every element, giving you access to both the value and its index. When you need to create a new array based on the original, such as converting all strings to uppercase, use the map method. map applies a function to each element and returns a new array with the results, without changing the original array.
These array methods help you build dynamic, interactive experiences by letting you easily update, display, and transform lists of data.
Bedankt voor je feedback!