Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

preface

After watching the array video, clean it up while it’s hot

Array methods

ForEach () map() filter() some() every()

forEach()

Array.prototype.foreach () traverses Array, returns no value, just traverses Array

let arr = ["Green pepper"."Eggs"."Cucumber"."Programmer"];
arr.forEach((value, index, array) = > {
    console.log(index + 1 + ":" + value);
    console.log(array);
})
Copy the code

The print result is as follows:

  • Value: Gets the contents of the current element in the array
  • Index: Gets the index of the current element
  • Array: Gets the entire array
  • There is no return value

Implement the addition of each element

let arr = [2.5.8.11];
let num = 0;
arr.forEach(value= > num += value)
console.log("num:" + num);      / / 26
Copy the code

map()

The array.prototype.map () method creates a new Array with the result that each element in the Array is the value returned by calling the provided function once. Similar to forEach().

  • A map function can be thought of as a mapping function, and a one-to-one mapping
  • A map is suitable for performing the same operation on every element in an array

Implement all element values *2

const array1 = [1.4.9.16];
const map1 = array1.map(x= > x * 2);
console.log(map1);
// Print the result: [2, 8, 18, 32]
Copy the code

filter()

The array.prototype.filter () method creates a new Array containing all the elements of the test implemented by the provided function.

  • The filter function can be thought of as a filter function that returns an array of elements that match the criteria
  • Filter (map) : filter (map) returns the element if it is true or false. Map does not have this procedure.
  • The filter function is suitable for filtering elements in an array that meet the criteria. Note that the filter function is only a filter function, and cannot change or manipulate elements
  • Filter: Filter. After the filter array, returns the filtered new array

Filter elements by character length

const words = ['spray'.'limit'.'elite'.'exuberant'.'destruction'.'present'];
const result = words.filter(word= > word.length > 6);
console.log(result);
// Print result: ["exuberant", "destruction", "present"]
Copy the code

Filter elements by numeric values

const arr = [4.8.11.3.1.9];
const result = arr.filter(value= > value > 5)
console.log(result);
// Print the result: [8, 11, 9]
Copy the code

some()

The array.prototype.some () method tests that at least one element in the Array passes the provided function test. It returns a Boolean value.

  • Used to check for the presence of data, returning true if it exists and false otherwise

Determines whether the element exists

const array = [1.2.3.4.5];
const even = (element) = > element % 2= = =0;
console.log(array.some(even));
// Prints the result: true
Copy the code

Some () is used only to determine the presence of an element, and as soon as the element is detected, it stops traversal and returns true

every()

The array.prototype.every () method tests whether all elements in an Array pass the test of a given function. It returns a Boolean value.

Determine whether all elements satisfy the condition

const array = [1.2.3.4.5];
const even = (element) = > element % 2= = =0;
console.log(array.every(even));
// Prints the result: false
Copy the code

conclusion

  • Every day after learning to have some small summary, come on! Dig the friends!
  • Subsequent update: JS prototype chain