How JavaScript creates a new object from a reference type (Array, object)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.