Merging of objects
Requirements: With Object O1, O2, need to get objects O3
var o1 = {A: ' A '}, O2 = {b: ' B ' }; // the var o3 = {A: ' A ', B: ' B '}
Method 1: Use the Extend method of jquery
* * Method definition **:jquery.extend ([deep], Target, Object1, [objectn])
> extends an object with one or more other objects, returning the object being extended.
> If target is not specified, the jquery namespace itself is extended. This helps plug-in authors add new methods to jquery. If the first argument is set to True, jquery returns a deep copy, recursively copying any objects found (recursive merge). Otherwise, the copy will share the structure with the original object. Undefined properties will not be copied, but properties inherited from the object's prototype will be copied.
O3 = $.extend (O1, O2) // merge O1 and O2 to return the results to O3. Note: At this point, O1 = = o3! is modified / / or / merge O1 and O2 to return the results to O3. Note: At this time, O1! = o3! That O1 has not been modified
Method 2: Use Object.assign (); :
var o1 = {a:1 }; var o2 = {b:2 }; var o3 = {C:3var obj =// {a:1, b:2, c:3}Console.log (O1);
//
{a:1, b:2, c:3}, notice that the target object itself also changes.
//
{b:2}
//
{c:3}
As with $.extend, the objects are merged into the first object.
Method 3: Traverse the Assignment method
Code logic:
- Each of the corresponding properties in the Loop object N.
- Confirm that the attribute exists in object n
- Confirm that the property does not exist in object o
var extend=function(o,n) { for (var in N) { if (N.hasownproperty (P) && (! O.hasownproperty (P))) o[p]=n[p];
Similar to the direct assignment increment attribute:
o3=o1;o3[' B ']= ' B '; // O3 ={O1: ' A ', O2: ' B '};
Note: Typescript is currently used in this way.
More:
JS Gets the folder full path where the current file is located
The difference between JS escape (), encodeURI (), and encodeURIComponent ()
JS to determine whether the picture is loaded three ways
Javascript objects (object) Merge