jquery merge objects

Solutions on MaxInterview for jquery merge objects by the best coders in the world

showing results for - "jquery merge objects"
Fabio
25 Jun 2016
1//merging two objects into new object
2var new_object = $.extend({}, object1, object2);
3
4//merge object2 into object1
5$.extend(object1, object2);
6
Simone
12 Feb 2017
1<!doctype html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <title>jQuery.extend demo</title>
6  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
7</head>
8<body>
9 
10<div id="log"></div>
11 
12<script>
13var object1 = {
14  apple: 0,
15  banana: { weight: 52, price: 100 },
16  cherry: 97
17};
18var object2 = {
19  banana: { price: 200 },
20  durian: 100
21};
22 
23// Merge object2 into object1
24$.extend( object1, object2 );
25 
26// Assuming JSON.stringify - not available in IE<8
27$( "#log" ).append( JSON.stringify( object1 ) );
28</script>
29 
30</body>
31</html>
32