Learn forEach Array Method in Javascript
buayaberdiri.blogspot.com - The forEach() method is an array method that is used to execute a function on each element of an array. It is defined as follows:
Example code :
arr.forEach(callback(currentValue[, index[, array]])[, thisArg]);
Here, callback is a function to execute on each element of the array. It takes three arguments:
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed in the array.
- array (optional): The array that forEach() is being applied to.
- thisArg (optional) is a value to use as this when executing the callback.
Here is an example of using forEach() to print out all the elements of an array:
Example code :
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
console.log(number);
});
// Output:
// 1
// 2
// 3
// 4
// 5
You can also use an arrow function as the callback function:
Example code :
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(number => console.log(number));
// Output:
// 1
// 2
// 3
// 4
// 5
Note that forEach() does not return a value and is used for side effects. If you want to transform the elements of an array and return a new array, you can use the map() method instead.
Learn Array.map()
The map() method is an array method that is used to transform the elements of an array. It is defined as follows:
Example code :
const newArray = arr.map(callback(currentValue[, index[, array]])[, thisArg])
Here, callback is a function that is applied to each element of the array. It takes three arguments:
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed in the array.
- array (optional): The array that map() is being applied to.
- thisArg (optional) is a value to use as this when executing the callback.
map() returns a new array with the results of calling the callback function on every element in the array.
Here is an example of using map() to create a new array with the square of each element in an array:
Example code :
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(number => number * number);
console.log(squares);
// Output: [1, 4, 9, 16, 25]
You can also use a function expression as the callback function:
Example code :
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(function(number) {
return number * number;
});
console.log(squares);
// Output: [1, 4, 9, 16, 25]
Note that map() does not modify the original array. It creates a new array with the transformed elements.
You can use the map() method with any type of array, not just arrays of numbers. For example, you can use it to transform an array of strings or an array of objects.
Learn Array.filter()
The filter() method is an array method that is used to create a new array with elements that pass a test specified in a callback function. It is defined as follows:
Example code :
const newArray = arr.filter(callback(currentValue[, index[, array]])[, thisArg]);
Here, callback is a function that is applied to each element of the array. It takes three arguments:
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed in the array.
- array (optional): The array that filter() is being applied to.
- thisArg (optional) is a value to use as this when executing the callback.
filter() returns a new array with all elements that pass the test implemented by the callback function. The test is a boolean function, and it should return true if the element should be included in the new array, and false otherwise.
Here is an example of using filter() to create a new array with only the even numbers from an array:
Example code :
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers);
// Output: [2, 4]
You can also use a function expression as the callback function:
Example code :
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(evenNumbers);
// Output: [2, 4]
Note that filter() does not modify the original array. It creates a new array with the elements that pass the test.
You can use the filter() method with any type of array, not just arrays of numbers. For example, you can use it to filter an array of strings or an array of objects.
Learn Array.reduce()
The reduce() method is an array method that is used to reduce the elements of an array to a single value. It is defined as follows:
Example code :
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
Here, callback is a function that is applied to each element of the array. It takes four arguments:
- accumulator: The accumulator accumulates the callback's return values. It is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed in the array.
- array (optional): The array reduce() was called upon.
initialValue (optional) is a value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initialValue and the callback will be called for all elements in the array.
reduce() applies the callback function to each element in the array, left to right, to reduce the array to a single value. The return value of the callback function is the accumulated result, and is provided to the next call as the accumulator argument.
Here is an example of using reduce() to sum all the elements in an array:
Example code :
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum);
// Output: 15
You can also use a function expression as the callback function:
Example code :
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum);
// Output: 15
Note that reduce() does not modify the original array. It returns a single value that is the result of the reduction.
You can use the reduce() method with any type of array, not just arrays of numbers. For example, you can use it to concatenate all the elements in an array of strings or to create an object from an array of objects.
Check out other articles about HTML & CSS:
- Learn Input Types In HTML For Login And Register Form
- Learn HTML List - How To Create Square, Circle, Number List
- How To Create Comments Line In HTML
- How to Add Text Formatting In HTML
- Adding The Fieldset Code To The Form In HTML
- How to Create a Search Box in Pure HTML
- Create Color Charts With Pure HTML
- How to change font size using CSS
- Types of Border Styles in CSS Code
- How to Change the Background Color in CSS
- Learn How To Create CSS Gradient for Background Color
List of Article Posts https://buayaberdiri.blogspot.com
- What is Breadcrumb Navigation For Website
- What is Rank Math Plugin in Wordpress
- Creating an author account in WordPress
- Configuring your domain and hosting with GoDaddy and WordPress
- How to create a sitemap in google search console
- Why A Sitemap On A Website Is So Important
- How to get your website approved by google adsense
- What is Domain Authority (DA) Website And How To Increase
- How Does CPM Work in Google Adsense And Differents With CPC
- Rejection of Google Adsense approval due to low value content, how to fix ?
- What is Google Spam Update And Impact To Your Website
- Admin Executive Jobs in MYLIFESTYLE HOLDINGS PTE. LTD
- Engineer Assistant Jobs Daifuku Mechatronics (S) Pte Ltd
- Customer Service Officer Jobs In Lending Bee Pte Ltd
- Clinic Executive Jobs in The Orthopaedic Centre (TOC)
- Student Recruitment Jobs in Singapore University of Social Sciences
- Healthcare Assistant Jobs in Acumed Medical Pte Ltd
- Operator Jobs in CMC Materials Singapore Pte. Ltd
- Assistant Housekeeping Manager Jobs in Four Seasons Hotel Singapore
- Office Cleaner Jobs in PERSOLKELLY Singapore Pte Ltd
- Line Room Attendant Jobs in Paradox Hotel
- Housekeeping Supervisor Jobs in Copthorne King's Hotel
- Housekeeper Jobs in Pure Group (Singapore)
- Risk Management Group Jobs in DBS Bank Limited
- Cloud Specialist Jobs in DBS Bank Limited
- DBS Bank Limited : Job Vacancies, Address, Contact
- Administrative Assistants Jobs in Tan Eng Huat Motor
- Customer Service Jobs in Tien Hsia Language School
- Customer Service Jobs in Yong Fah International Pte Ltd
- Administrative Officer Part time Jobs in SDC
- Warehouse Assistant Job Vacancy in Okonz Pte. Ltd
- Ringkasan Tentang Tujuan Dari Manajemen Keuangan
- Pengertian dan Konsep Dasar Keuangan
- Lowongan Operator Dan Staff PT Andalan Sapta Mandiri
- Lowongan Kerja SMA SMK D3 dan S1 Terbaru
- Lowongan PT Garuda Metal Utama Posisi Operator & Staff
- Lowongan Operator dan Staff PT Samudra Utama Narapati
- Lowongan Kerja Staff Produksi PT Citra Sinergi Jakarta
- Lowongan Kerja PT Maxxis International Indonesia Posisi Operator dan Staff
- Lowongan PT Tongcheng Magnet Wire Posisi Operator dan Staff
- Lowongan Kerja Operator Produksi PT Intera Lestari Polimer
- Lowongan Kerja PT NSK Bearings Posisi Production Specialist
- Lowongan Kerja Staff QA PT Tirta Alam Segar (TAS)
- Lowongan Operator Maintenance PT TUFFINDO NITTOKU AUTONEUM
- Lowongan Kerja Staff Produksi di PT Panasonic Gobel
- Lowongan Kerja Operator Produksi di PT Delcoprima Pacific
- Lowongan PT PQ Silicas Indonesia Posisi Utility Operator
- Lowongan Kerja Dailywell Posisi Staff Produksi & Operator Mesin
- Lowongan Maintenance Manager di PT Lotte Indonesia
- Lowongan Kerja Operator Forklift di PT Lotte Indonesia
- Lowongan Kerja Operator Produksi PT Eslar Utama
- Lowongan Kerja Administrasi Di PT Kalbe Farma Tbk
- Lowongan Kerja PT Excel Metal Industry Melalui Email dan Website Resmi
- Lowongan Kerja Network Engineer di Bank BCA
- Lowongan Kerja Database Engineer di Bank BCA
- Lowongan Kerja Application Developer di Bank BCA
- Lowongan Kerja Data Center System Bank BCA
- Lowongan Kerja DevOps Engineer di Bank BCA
- Lowongan Kerja Operator dan Non Operator PT TSSI
- Lowongan Kerja Quality Control Staff di Pabrik Baterai ABC
- Lowongan Kerja Staff Admin di PT Bevananda Mustika
- Lowongan Kerja Staff PPIC di PT Allure Alluminio
- Lowongan Kerja PT Lion Wings Posisi Staff Produksi
- Lowongan Kerja PT. Schlemmer Automotive Indonesia
- Lowongan Kerja Staff Teknik di PT HPPM (Honda Precision Parts Manufacturing)
- Lowongan Pekerjaan PT ARYA XPRESS Posisi Supir Expedisi (Driver)
- Lowongan Kerja Driver di PT Akazora Dinamis Mandiri
- Lowongan Kerja PT Mayora Indah Tbk Lewat Email dan Website Resmi
- Lowongan Kerja Driver atau Supir PT Hongfa Electronic Indonesia
- Lowongan Kerja Cleaning Service di CV Hello Klin Indonesia
- Lowongan Pekerjaan PT Sinar Sosro dan Alamat Email
- Lowongan Pekerjaan Operator di PT Tangkas Cipta Optimal (TACO)
- Lowongan Pekerjaan Operator Produksi di PT Tirta Alam Segar
- Info Loker Operator Warehouse di PT Nippo Mechatronics Indonesia
- Lowongan Kerja Staff QC di CV Golden Star Bird Nest
- Lowongan Kerja Posisi TEXTILE ENGINEER di PT Mattel Indonesia
- Alamat Email PT Mattel Indonesia Untuk Melamar Pekerjaan
- Info Loker PT Cendrawasih Pertiwijaya (Blue bird Cijantung)
- Loker Cleaning Service di PT Halilintar Lintas Semesta atau HLS Telecom
- Cara Melamar Pekerjaan Driver di Bluebird Pool Sutoyo
- Loker Driver atau Supir Direksi di PT Panca Budi Pratama
- Loker Supir atau Driver di Style Korean Indonesia
- PT Kiyokuni Indonesia : Info Loker, Alamat Pabrik, BKK
- Cara Melamar Pekerjaan PT Denso Indonesia di Website Resmi
- PT Mesin Isuzu Indonesia : Info Loker, Alamat, Email
- PT MITSUBA INDONESIA PIPE PARTS : Info Loker, Email, Alamat Lengkap
- PT. Shindengen Indonesia : Lowongan Pekerjaan, Email, Alamat Lengkap
- Info Loker PT Suryaraya Rubberindo Industries (SRI) , Alamat, BKK Sekolah
- PT. TUFFINDO NITTOKU AUTONEUM : Info Loker, Email, Alamat Lengkap
- PT Kawasaki Motor Indonesia : Info Loker, BKK Sekolah, Alamat Pabrik
- Info Loker PT EPSON Indonesia : Alamat, Email, Nomor Telepon & Yayasan
- Cara Melamar Pekerjaan di PT AHM : Info Loker, Email, Alamat
- Loker PT Indofood : Lowongan Kerja, Alamat, Email, Nomor Telepon
- PT Yamaha Motor : Lowongan Kerja, Alamat, Nomor Telepon
- PT PLUMBING SOLUSI INDONESIA - Lowongan Kerja, Alamat Dan Email
- PT Cahaya Arif Abadi - Info Loker, ALamat dan Bergerak di bidang apa
- IMF Request to Indonesia to Cancel Ban on Export of Raw Minerals
- How to Implement the Integration AMP in Google Analytics 4
- iOS 17 For iPhone : New Features, Device Support, How to Upgrade
- Elon Musk Investment Plan For Tesla in India and Indonesia