jQuery / jQuery Utility Methods

Merging and Extending Objects with $.extend()

This tutorial will teach you how to merge and extend objects using the $.extend() method in jQuery. Learning how to combine objects can be a great addition to your web development…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores jQuery utility functions for working with arrays, objects, and more.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Watermark Generator

Add watermarks to images easily.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help