This article will introduce some commonly used array processing functions and syntax, such as reduce(), filter(), map(), every(), some(), and spread operator. This knowledge is not directly related to React and Redux itself, but the examples in this chapter include the usage of these functions and syntax, so we can learn them in the program. It is also worth mentioning that the ideas of functions such as reduce(), filter(), map() come from functional programming. Thanks to JavaScript being a functional programming language, the originally complex logic processing has become so simple. reduce()Overview The reduce() method receives a function as an accumulator, and each value in the array (from left to right) is merged to form a single value. grammar array.reduce(callback,initialValue) The parameters are explained below. const completedCount = todos.reduce( (count,todo)=>(todo.completed?count+1:count), 0); Code explanation: todos is an array. filter()Overview The filter() method tests all elements with the specified function and creates a new array containing all elements that pass the test. grammar array.filter(callback,thisArg) The parameters are explained below. callback: A function used to test each element of the array, containing three parameters. Return true to keep the element (pass the test), false to not keep it. return state.filter(todo=> todo.id!==action.id ) Code explanation: state is an array of tasks. map()Overview The map() method returns a new array consisting of the return values of calling a specified method on each element in the original array. grammar array.map(callback, thisArg) The parameters are explained below. callback: This method is called on the element in the original array and returns a new element. This method contains the following three parameters. Example return state.map(todo=>( todo.id === action.id? Object.assign({},todo.{ text:action.text}): todo )); Code explanation: state is the state before the change, which is an array. The syntax is Object.assign(target,...sources). target is the target object, and sources is any number of source objects. every()Overview The every() method is used to test whether all elements in an array pass the test of the specified function. grammar array.every(callback,thisArg) callbak: The function used to test each element. Example const areAllMarked = state.every(todo=>todo.completed) When the task array is traversed and the completed attribute of each task is true, it returns true. some()Overview The some() method is used to test whether there is at least one element in the array that passes the test of the specified function. grammar array.some(callback[,thisArg]) The parameters are explained below. callback: The function used to test each element. Example const areAllMarked = state.some(todo=>todo.completed) Traverse the task array, and return true as long as there is a task whose completed property is true. Spread OperatorOverview The spread operator allows an expression to be expanded at some point. Common scenarios include: function parameters, array elements, and deconstruction assignment. grammar For function parameters: myFunction(...iterable0bj); For array elements: [...iterableobj,4,5,6] For destructuring assignment: const [a,b,...rest]=[1,2,3,4,5] const {a,b,...rest}={a:1,b:2,c:3,d:4} Props for React components: <App...iterable0bj/> Example Expand state return[ id:state.reduce((maxId,todo)=>Math,max(todo,id,maxId),-1)+1, { completed:false, text:action.text, }, ...state, ]; Expand each property of actions into the component. <TodoItem key={todo.id}todo={todo}{...actions}/> Summarize The reduce() method receives a function as an accumulator, and each value in the array (from left to right) is merged to form a single value. This is the end of this article about array processing in React and Redux. For more information about array processing in React and Redux, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: How to configure the OpenWRT development environment on Ubuntu 18.04 (physical machine)
1. Set firewall rules Example 1: Expose port 8080...
Table of contents The principle and function of l...
The default request header of the http1.1 protoco...
In MySQL, you can use the REVOKE statement to rem...
Table of contents Overview 1. Simple Example 1. U...
Table of contents 1. Stop MySQL Server first 2. U...
JSON data is displayed and formatted on the HTML ...
1. Command method Run the nginx service in the cr...
Table of contents Preface Introduction to QueryCa...
Table of contents 1. Auxiliary functions 2. Examp...
Preface This article mainly introduces the releva...
There are many purposes for exporting MySQL data,...
MySQL installation is divided into installation v...
Good morning everyone, I haven’t updated my artic...
Preface: In the daily use of the database, it is ...