About introducing multiple functions for array control
About “multiple functions for array control”
While testing my code, I realized that there are multiple functions for controlling arrays.
I would like to share them with you.
How to insert a value into an array
Array.prototype.push() adds one or more elements to the end of the array, and returns the new length of the array. Example
const animals = ["pigs", "goats", "sheep"];
const count = animals.push("cows");
console.log(count);
// Expected output: 4
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push("chickens", "cats", "dogs");
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
How to concatenate all the elements of an array into a single string?
Use Array.join(). Example.
const elements = ["Fire", "Air", "Water"];
console.log(elements.join());
// Expected output: "Fire,Air,Water"
console.log(elements.join(""));
// Expected output: "FireAirWater"
console.log(elements.join("-"));
// Expected output: "Fire-Air-Water"
How to iterate through all elements of an array in ascending order
Use forEach(). forEach() executes the given callback once for each element in the array, in ascending order. It does not run against index properties that have been deleted or uninitialized (for example, sparse arrays). callback is called with three arguments: the element value, the element index, and the array being traversed.
Example of skipping iteration over uninitialized values
const arraySparse = [1, 3, , 7];
let numCallbackRuns = 0;
arraySparse.forEach(function (element) {
console.log(element);
numCallbackRuns++;
});
console.log("numCallbackRuns: ", numCallbackRuns);
// 1
// 3
// 7
// numCallbackRuns: 3
// comment: as you can see the missing value between 3 and 7 didn't invoke callback function.
Example of replacing a for() loop with forEach()
const items = ["item1", "item2", "item3"];
const copy = [];
// before
for (let i = 0; i < items.length; i++) {
copy.push(items[i]);
}
// after
items.forEach(function (item) {
copy.push(item);
});
Example of controlling a specific index element of an array
const months = ["Jan", "March", "April", "June"];
// Inserts at index 1
console.log(months.splice(1, 0, "Feb"));
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, "May");
// Replaces 1 element at index 4
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]
console.log(months.splice(2, 2, "May"));
// Expected output: Array ["Jan", "Feb", "May"]
// Console output: Array ["March", "April"]
// Excluded elements are output as an array
console.log(months);
// Expected output: Array ["Jan", "Feb", "May", "May"]
``` Translated with www.DeepL.com/Translator (free version)