Created
March 9, 2016 21:44
-
-
Save csdear/79c9e704c50c5e5cb794 to your computer and use it in GitHub Desktop.
jquery .Extend will merge the contents of two or more objects into the first object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This will merge the contents of two or more objects into the first object | |
| // I think it can also be said, can be merged aggregated into a entirely new object. | |
| //target, the object will be extended, have the new properties. | |
| //HTML | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="https://code.jquery.com/jquery-2.1.4.js"></script> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>jQuery EXTEND Demo</title> | |
| </head> | |
| <body> | |
| <div id="log"></div> | |
| </body> | |
| </html> | |
| //JS | |
| var object1 = { | |
| apple : 0, | |
| banana: { weight: 52, price: 100 }, | |
| cherry: 97 | |
| }; | |
| var object2 = { | |
| banana: { price: 200 }, | |
| kiwi : 65, | |
| watermelon: 400, | |
| }; | |
| //Merge object2 into Existing object1 | |
| //$.extend( object1, object2 ); | |
| //Or create a new object to aggregate them in.. | |
| var newObject = $.extend( object1, object2 ); | |
| //Assuming JSON.stringify. selecting the log div. | |
| $("#log").append( "New Object Contents" + "</br>" + JSON.stringify(newObject)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment