This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Note: Part of the content and pictures from the network, if there is any infringement, please contact me (home page has the public number: Small siege city Lion science front end)
Author: small front siege city lion, home: small front siege city lion’s home page, source: Nuggets
GitHub: P-J27, CSDN: PJ wants to be a front end siege lion
The copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.
preface
In this article, we will read the contents of JS array in detail. This article is about “array traversal (1)”, and the previous article is about portal
The map () method
Function: Process each item in the array. Returns a new processed array. Run a callback function for each item in the array, returning the result of that function to form a new array (returning the processed array). It doesn’t change the array.
arr.map(function (item, index, arr) {
Β Β return newItem;
});
Copy the code
Example 1 :(changing the value of an array element while copying)
I have a given array, ARR1, and I want to add 10 to each element in ARR1, so I can use the map method.
var arr1 = [1.3.6.2.5.6];
var arr2 = arr1.map(function (item, index) {
Β Β return item + 10; // Add 10 to each element in arR1
});
console.log(arr2);
Copy the code
Print result:
Example 2: [Important case, often used in actual development]
Store the value of an attribute in array A into array B. Example code:
const arr1 = [
Β { name: 'A small leading Lion'.age: '18' },
Β { name: 'PJ wants to be the front siege lion '.age: '20'},];// Store the name attribute in array ARR1 to array ARR2
const arr2 = arr1.map((item) = > item.name);
β
// Add arR1, age, and name to arR3; // Add arR1, age, and name to arR3
const arr3 = arr1.map((item) = > ({
Β Β myName: item.name,
Β Β myAge: item.age,
})); // Store the name attribute in array ARR1 to array ARR2
β
console.log('arr1:' + JSON.stringify(arr1));
console.log('arr2:' + JSON.stringify(arr2));
console.log('arr3:' + JSON.stringify(arr3));
Copy the code
The main application scenario of map: is to make a processed copy of the original array data
Think: Is it true that the map() method doesn’t change the array?
Answer: not necessarily. For the forEach analysis above, item is a shallow copy. The map callback also takes three parameters, and it is possible to change the array if you want.
filter()
Function: Filters an array.
arr.filter(function (item, index, arr) {
Β Β return true;
});
Copy the code
Explanation: Run a callback function for each item in the array, returning a new array of filtered elements without changing the original array.
Example: Find elements greater than 4 in array ARR1 and return a new array.
let arr1 = [1.3.6.2.5.6];
let arr2 = arr.filter(item= > item > 4);
β
console.log(JSON.stringify(arr1)); // print result: [1,3,6,2,5,6]
console.log(JSON.stringify(arr2)); // print results: [6,5,6]
Copy the code
Thank you for reading, I hope it can help you, if there are mistakes or infringement of the article, you can leave a message in the comment area or add the public account in my home page to contact me.
Writing is not easy, if you feel good, you can “like” + “comment” thanks for supporting β€