google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






JavaScript Objects: Compare and Clone This JavaScript article tutorial shows you a short overview about JavaScript objects in comparing and cloning. You'll know how to clone JavaScript objects, make JavaScript object copy. This is really a should-read JavaScript tutorial when you would like to learn more advance JavaScript skills.


Generate your business videos by AI with voice or just text



Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
Free UNLMITED AI Video App in Your Hand

Here's a handy way to determine if two JavaScript objects are identical without using a framework like jQuery or MooTools:

var compare_objects = function (obj1, obj2){

 
    var parameter_name;
 
    var compare = function(objA, objB, param){

 
        var param_objA = objA[param],
            param_objB = (typeof objB[param] === "undefined") ? false : objB[param];

 
        switch(typeof objA[param]){
            case "object": return (compare_objects(param_objA, param_objB));

            case "function": return (param_objA.toString() === param_objB.toString());

            default: return (param_objA === param_objB);
        }

 
    };
 
    for(parameter_name in obj1){
        if(typeof obj2[parameter_name] === "undefined" || !compare(obj1, obj2, parameter_name)){

            return false;
        }
    }
 
    for(parameter_name in obj2){

        if(typeof obj1[parameter_name] === "undefined" || !compare(obj1, obj2, parameter_name)){

            return false;
        }        
    }
 
    return true;

 
};

Here's an easy way to clone a JavaScript object:

var clone_object = function (original_obj) {

    var new_obj = {};
    for(var param in original_obj) {

        if(original_obj.hasOwnProperty(param)){
            if(typeof(original_obj[param]) === "object"){

                new_obj[param] = clone_object(original_obj[param]);

            } else {
                new_obj[param] = original_obj[param];

            }
        }
    }
    return new_obj;
};

A real-world example of the two functions:

var object_1 = { fruit: "apple", tree: "dogwood", number: 3 };

var object_2 = { fruit: "apple", tree: "dogwood", number: 3, cartoons: { anime: "robotech", fantasy: "he-man" } };

var object_3 = clone_object(object_2);
 
console.log("objects 1 and 2 match? " +compare_objects(object_1, object_2));

//displays false;
 
console.log("objects 2 and 3 match? " +compare_objects(object_2, object_3));

//displays true

Bear in mind both of these functions are recursive, which means the larger the object, the slower the performance.

AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app

Your next top AI Assistant
Claude AI, developed by Anthropic

Your next top AI Assistant
Claude AI, developed by Anthropic

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web