🎧 Spotlights
JavaScript Array Functions
Refreshing GoLang
In December 2009, ECMAScript 5 was published, and a new set of array methods was introduced to improve the existing ones. Yet, these methods gained little popularity due to the need for browser support for ES5. Although these methods were practical, developers did not consider the effort required to write a polyfill for them to be worth it. So, they were considered "good-to-have" instead of "must-have." Some people even called these methods as Array
"Extras".
Snippets of Code is an engineering journal documenting learnings from experiments and projects. I am learning about various programming languages with Exercism’s. Please share or like if the content is helpful.
While these methods are only available to Array
, they can be helpful for Nodelist, jQuery Objects, and String. We can make these Array methods available to other objects by using a technique known as "de-methodizing." We de-methodize the forEach
method into a generic function call each
using Function.prototype.call.bind.
This each
function can now be used on objects other than Array.
The indexOf()
method searches an array and returns the first index where a given element is found. If the component is not present, it returns -1.
const fruits = ['apple', 'banana', 'orange', 'grape'];
console.log(fruits.indexOf('orange')); // Output: 2
console.log(fruits.indexOf('watermelon')); // Output: -1
If you are enjoying the learning journal, please ❤️
The filter()
method creates a new array that includes only the elements that pass a given test. The test is performed by the function that is provided as an argument.
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4, 6]
The forEach()
method executes a provided function once for every element in the array. Using forEach()
is recommended over using for
loops, as it offers many benefits. One of the most significant advantages is that variables declared within the loop are local to the loop and are not accessible outside of it. This makes forEach() it
safer and less prone to bugs. Yet, according to jsperf, for
loops are faster than forEach()
speed.
const colors = ['red', 'green', 'blue'];
colors.forEach(color => console.log(color));
// Output:
// red
// green
// blue
The map()
method creates a new array by calling a function provided to each element in the original array. The function's result is added to the new array. It is often the preferred method in applications that rely on JavaScript to manipulate data from a server.
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map(num => num * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
If you think someone will benefit from this content, please share.
The reduce()
method applies a function to each element in the array and uses the result as the accumulator for the next element. This process continues until the array has been reduced to a single value.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 15
💻 👓
package partyrobot
import "fmt"
// Welcome greets a person by name.
func Welcome(name string) string {
return "Welcome to my party, " + name + "!"
}
// HappyBirthday wishes happy birthday to the birthday person and exclaims their age.
func HappyBirthday(name string, age int) string {
return fmt.Sprintf("Happy birthday %s! You are now %d years old!", name, age)
}
// AssignTable assigns a table to each guest.
func AssignTable(name string, table int, neighbor, direction string, distance float64) string {
// want: "Welcome to my party, Chihiro!\nYou have been assigned to table 022. Your table is straight ahead, exactly 9.2 meters from here.\nYou will be sitting next to Akachi Chikondi.",
return fmt.Sprintf("Welcome to my party, %s!\nYou have been assigned to table %03d. Your table is %s, exactly %.1f meters from here.\nYou will be sitting next to %s.", name, table, direction, distance, neighbor)
}
This code imports the fmt
package, which is a standard Go package that provides formatted I/O functions.
The Welcome
function takes a name
parameter of type string and returns a greeting message welcoming the person to the party. It uses string concatenation to form the greeting.
The HappyBirthday
function takes two parameters: name
, a string representing the birthday person's name, and age
, an integer representing their age. It uses fmt.Sprintf
to format a birthday message with the person's name and age.
The AssignTable
function assigns a table to a guest at the party. It takes five parameters: name
, a string representing the guest's name; table
, an integer representing the assigned table number; neighbor
, a string representing the name of the guest sitting next to them; direction
, a string representing the direction of the table from the guest, and distance
, a float64 representing the distance of the table from the guest. It is used fmt.Sprintf
to format a message with all the information about the table assignment.
🧪 Next Actions
Keep refreshing Golang even when my writing won’t be focused on it.