array additions and deletions
1. Add a new
You can use the Concat method, which does not alter the original array, but instead creates a new array
Let A = [0, 1, 2= A.concat ([3]) Console.log (A, b)
2. Delete one item
Splice also does not satisfy the requirement to delete an item, because the method changes the original array, we should use slice and combine the new features of ES next.
Let array = [array.slice= (array, index) = = {return [ ...] (0 , index), + 1), ] = Removeindex (array, 1);
3. Update one item
Let array = [1, 2, 3= (array, index, CB) = = {return [ ... array.slice (0
CB (Array[index] ), + 1
), ]
function
(data) {
return data + 1
;});
4, new can also use this idea, than concat to be flexible
Let array = [1, 2, 4= (array, index, value) = = {return [ ... array.slice (0 , index), value, -1), ] 2, 3);
Search and delete of objects
1. Update one item
Use the object.assign method of the new feature of Es next.
The first argument must be an empty object, because the first parameter will be changed (accept the values of all parameters)
The value we want to update must be placed on the last side, because the more the parameter, the higher the permissions. To overwrite the contents of the former duplicates.
Let SourceItem = { 0, ' Learn Redux ', false}object.assign ({}, SourceItem, { true})
2. Object operation: Add a new item
In fact, as in the above, write a new key value pair even if new
Let SourceItem = { 0, ' Learn Redux ', false} Object.assign ({}, SourceItem, { 123})
3. Object operation: Delete an item
Let SourceItem = { 0, ' Learn Redux ', false, 123 = Object.key (SourceItem). reduce ((obj, key) = { if (key!== ' fuck ') { return {... obj, [key], Sourceitem[key]} } return obj}, {})
How JavaScript creates a new object from a reference type (Array, object)