Merging and Extending Objects with $.extend()

Tutorial 3 of 5

Merging and Extending Objects with $.extend() in jQuery

1. Introduction

Goal

This tutorial aims to teach you how to use the $.extend() method in jQuery to merge and extend JavaScript objects.

Learning Outcomes

By the end of this tutorial, you will be able to:

  • Understand the concept behind merging and extending objects
  • Use the $.extend() method effectively to combine and extend objects
  • Apply best practices when using this method

Prerequisites

Basic knowledge of JavaScript and jQuery is required. You also need to have jQuery library installed or a CDN link to jQuery in your HTML file.

2. Step-by-Step Guide

The $.extend() function in jQuery is used to merge the contents of two or more objects into the first object. The function's syntax is as follows:

$.extend(target, object1, object2, ..., objectN)
  • target is the object that will receive the new properties.
  • object1, object2, ..., objectN are the objects containing properties to be merged into the target object.

Deep Copy

If the first argument passed to $.extend() is true, a deep copy is performed. This means that properties in the source objects are recursively copied into the target object.

$.extend(true, target, object1, object2, ..., objectN);

3. Code Examples

Example 1: Simple Merge

var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};

var object2 = {
  banana: { price: 200 },
  durian: 100
};

// Merge object2 into object1
$.extend(object1, object2);

console.log(object1);

The output will be:

{
  apple: 0,
  banana: { price: 200 },
  cherry: 97,
  durian: 100
}

Example 2: Deep Merge

var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};

var object2 = {
  banana: { price: 200 },
  durian: 100
};

// Perform a deep merge
$.extend(true, object1, object2);

console.log(object1);

The output will be:

{
  apple: 0,
  banana: { weight: 52, price: 200 },
  cherry: 97,
  durian: 100
}

In a deep merge, the properties of banana in object1 and object2 are merged together.

4. Summary

In this tutorial, we've learned how to:

  • Merge and extend objects using the $.extend() method.
  • Perform a deep merge with $.extend().
  • Understand the difference between a simple merge and a deep merge.

For further learning, you can explore other jQuery methods for manipulating objects and arrays.

5. Practice Exercises

Exercise 1

Write code to merge the following objects into one:

var object1 = {
  cat: 10,
  dog: 20
};

var object2 = {
  bird: 30,
  fish: 40
};

Solution

$.extend(object1, object2);
console.log(object1);

Exercise 2

Perform a deep merge on the following objects:

var object1 = {
  cat: 10,
  dog: { weight: 20, price: 100 }
};

var object2 = {
  dog: { price: 200 },
  bird: 30
};

Solution

$.extend(true, object1, object2);
console.log(object1);

Tips for Further Practice

Try to create more complex objects and practice merging them. Play around with deep and simple merges to fully understand their differences.