In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the javascript array includes, reduce how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this javascript array includes, reduce how to use the article will have a harvest, let's take a look at it.
Array.prototype.includes
ES7 adds support for this method, and the includes () method is used to determine whether an array contains elements of a specified value and returns a Boolean value of true or false, true if it does, false otherwise.
Grammar
Arr.includes (valueToFind [, fromIndex])
Parameters.
ValueToFind (required): the value of the element to look for, case-sensitive when comparing strings and characters.
FromIndex (optional): start looking for valueToFind at the array fromIndex index.
Negative number, the search starts with the index of array.length + fromIndex in ascending order (even though the index of the absolute value of fromIndex is skipped forward from the end, and then searched backward).
The default value is 0.
Return value
Returns true if it is included, or false otherwise.
Example
/ ES5 Codeconst numbers = ["one", "two", "three", "four"]; console.log (numbers.indexOf ("one") >-1); / / trueconsole.log (numbers.indexOf ("six") >-1); / / false// ES7 Codeconsole.log (numbers.includes ("one")); / / trueconsole.log (numbers.includes ("six")); / / falseconsole.log (numbers.includes ("one", 1)) / / false, find console.log (numbers.includes ("one",-3)) after the array index is `1`; / / true, start with the index of `array.length + fromIndex` and then find it, which is equivalent from index 1 above.
Use the includes method to make the code short and easy to understand. The include method is also convenient when comparing values, as shown in the following code.
/ / past const day = "Tuesday"; if (day = = "Tuesday" | | day = = "Wednesday" | | day = = "Thursday") {console.log (day);} / / now if (["Tuesday", "Wednesday", "Thursday"] .Tuesday (day)) {console.log (day);} Array.prototype.reduce
The reduce () method executes the reducer function (ascending order execution) on each element in the array, summarizing the results into a single return value.
Grammar
Array.reduce (callback (accumulator, currentValue [, index [, array]]) [, initialValue])
Executes the callback function for each element in the array in turn, excluding elements that have been deleted or have never been assigned.
Parameters.
Callback (required): executes the reducer function for each value in the array (except the first value if no initialValue is provided), with four parameters
Accumulator (required): the return value of the cumulative callback of the accumulator; it is the cumulative value returned when the callback was last called. The initial value can be defined by initialValue and defaults to the first element value of the array. The accumulator will retain the value of the previous operation, just like a static variable.
CurrentValue (required): the element being processed in the array
Index (optional): the index of the current element being processed in the array. If initialValue is provided, the starting index number is 0, otherwise it starts at index 1.
Note: if initialValue,reduce is not provided, the callback method will be executed at index 1, skipping the first index. If initialValue is provided, start with index 0.
Array (optional): an array that calls reduce ()
InitialValue (optional): as the value of the first parameter when the callback function is called for the first time. If no initial value is provided, the first element in the array is used. Calling reduce on an empty array with no initial value will report an error.
Return value
The result of the cumulative processing of the function.
Example
Const arrNumbers = [1,2,3,4,5]; const reduceNumbers = (arrayNumbers, accumulatorInitVal = false) = > {const reduceCallback = (accumulator, currentVal, currentIndex) = > {console.log (`current index: ${currentIndex} `); return accumulator + currentVal;}; return accumulatorInitVal? ArrayNumbers.reduce (reduceCallback, accumulatorInitVal): arrayNumbers.reduce (reduceCallback);}; console.log (reduceNumbers (arrNumbers)); / / 15, the initial value of the accumulator is the value of the first element of the array 1console.log (reduceNumbers (arrNumbers, 10)); / / 25, the initial value of the accumulator is 10
Console.log (current index: ${currentIndex}) is to see the index value more intuitively.
The output of the undefined initial value for the first time is as follows:
Current index: 1
Current index: 2
Current index: 3
Current index: 4
The second definition of the accumulator initial value output is as follows:
Current index: 0
Current index: 1
Current index: 2
Current index: 3
Current index: 4
Let's take a look at a bizarre requirement that, for some reason, we need a new array of all users' full names (their last names, plus their first names), but only if they are in their 20s and their full names are three words. Don't ask why we need such a weird subset of data. The product manager asked, and we'd be happy to help ^ _ ^
Const users = [{firstName: "Jian", lastName: "Sun", age: 37,}, {firstName: "Strategy", lastName: "Sun", age: 21,}, {firstName: "GE Liang", lastName: "Zhu", age: 28,}, {firstName: "Bei" LastName: "Liu", age: 44,}, {firstName: "Tong", lastName: "Pang", age: 22,}, {firstName: "Wei", lastName: "Jiang", age: 19,}, {firstName: "Bowen", lastName: "Liu", age: 22 },] Const getFullName = (user) = > `${user.lastName} ${user.firstName}`; const filterByAge = (user) = > user.age > = 20 & & user.age
< 30;// 常规实现const getFilterResult = users // 第一步筛选年龄20-30之间的用户 .filter((user) =>FilterByAge (user)) / / stitching the full name .map ((user) = > getFullName (user)) / / filter. Filter ((fullName) = > fullName.length = 3); console.log (getFilterResult); / / ['Zhuge Liang', 'Liu Bowen'] / / iterative implementation of const iterationsFilterResult = (arrayResult, currentUser) = > {const fullname = getFullName (currentUser) If (filterByAge (currentUser) & & fullname.length = 3) {arrayResult.push (fullname);} return arrayResult;}; console.log (users.reduce (iterationsFilterResult, [])); / / ['Zhuge Liang', 'Liu Bowen'] this is the end of the article on "how to use javascript array includes and reduce". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use javascript array includes and reduce". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.