Detailed explanation of the new array methods in JavaScript es6

Detailed explanation of the new array methods in JavaScript es6

1. forEach()

Traverse the array without return. Even if there is return, no value will be returned and the original array will be affected.

callback parameters

value -- the value of the current index

index --index

arr -- original array

		let arr = ["a", "b", "c", 1, 2, 3];
        
        arr.forEach((value, index, arr) => {
            console.log(value, index, arr);
        })

Output:

insert image description here

2. arr.filter()

Filter an array and return an array that meets the requirements

callback parameters:

value -- the value of the current index

index --index

let arr = [1,2,3,4,5]
let arr1 = arr.filter( (value, index) => value%2 === 0)
console.log(arr1) // [2, 4]

3. arr.every()

According to the judgment condition, whether all elements of the array meet the requirements, if so, return true

callback parameters:

value -- the value of the current index

index --index

let arr = [1,2,3,4,5]
let arr1 = arr.every( (value, index) =>value<2)
console.log(arr1) // false
let arr2 = arr.every( (value, index) =>value<6)
console.log(arr2) // true

4. arr.map()

Map an array (traverse the array), and return a new array.

callback parameters:

value -- the value of the current index

index --index

array -- the original array

let arr = [1,2,3,4,5]
arr.map( (value,index,array)=>{
        value = value * 2
        console.log(`value:${value} index:${index} array:${array}`)
})   
console.log(arr)

result:

insert image description here

var arr1 = [1,2,3,4]; 
var res1 = arr1.map((item,index,arr)=>{ 
 item = item * 3; 
 return item; 
})
console.log(arr1); // [1,2,3,4]
console.log(res1); // [3,6,9,12]

5. arr.some()

According to the judgment condition, whether one of the elements of the array meets the condition, if one meets the condition, return true

callback parameters:

value -- the value of the current index

index --index

let arr = [1,2,3,4,5]
let arr1 = arr.some( (value, index) => value < 3)
console.log(arr1) // true
let arr2 = arr.some( (value, index) => value > 6)
console.log(arr2) // false

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Common array operation methods in JavaScript, including ES6 methods
  • es6 js method to match two array objects

<<:  CSS3 sets a mask for the background image and solves the problem of mask style inheritance

>>:  Teach you how to use Nginx service to build a subdomain environment to improve the loading performance of 2D maps

Recommend

Detailed tutorial on installing Hbase 2.3.5 on Vmware + Ubuntu18.04

Preface The previous article installed Hadoop, an...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

Getting Started with Nginx Reverse Proxy

Table of contents Overview The role of reverse pr...

Robots.txt detailed introduction

Basic introduction to robots.txt Robots.txt is a p...

React tips teach you how to get rid of hooks dependency troubles

A very common scenario in react projects: const [...

JavaScript implements mouse drag to adjust div size

This article shares the specific code of JavaScri...

React Native startup process detailed analysis

Introduction: This article takes the sample proje...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

A brief discussion on the pitfalls of react useEffect closure

Problem code Look at a closure problem code cause...

A brief introduction to JavaScript arrays

Table of contents Introduction to Arrays Array li...

CSS draw a lollipop example code

Background: Make a little progress every day, acc...

Example of building a Jenkins service with Docker

Pull the image root@EricZhou-MateBookProX: docker...