Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:01:36+00:00 2026-06-11T16:01:36+00:00

As we know that jQuery.extend(true, obj1, obj2) method for deep copying the object’s properties

  • 0

As we know that jQuery.extend(true, obj1, obj2) method for deep copying the object’s properties from obj2 to obj1. In case of array, it copies the property based on index. But I need copying based on some property (e.g. id in my case) as per following example:

obj1 = [{id:"id1", name:"name1"},{id:"id2", name:"name2"}]
obj2 = [{id:"id3", name:"name3"}{id:"id1", name:"name1_modified"}]

jQuery.extend will return:

[{id:"id3", name:"name3"}{id:"id1", name:"name1_modified"}]

But I need output as:

[{id:"id1", name:"name1_modified"},{id:"id2", name:"name2"}{id:"id3", name:"name3"}]

is there any method/library to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:01:37+00:00Added an answer on June 11, 2026 at 4:01 pm

    I have faced same problem while working on my project. So I jquery extend to accomplish array merge based on its property. If you want to merge array based on property, pass property name as last parameter in merge function. I have create a jsfiddle, see result in browser console.

    function merge() {
        var options, name, src, copy, copyIsArray, clone, targetKey, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false;
        var currentId = typeof arguments[length - 1] == 'string' ? arguments[length - 1] : null;
        if (currentId) {
            length = length - 1;
        }
        // Handle a deep copy situation
        if (typeof target === "boolean") {
            deep = target;
            target = arguments[1] || {};
            // skip the boolean and the target
            i = 2;
        }
    
        // Handle case when target is a string or something (possible in deep
        // copy)
        if (typeof target !== "object" && !jQuery.isFunction(target)) {
            target = {};
        }
    
        // extend jQuery itself if only one argument is passed
        if (length === i) {
            target = this;
            --i;
        }
    
        for (; i < length; i++) {
            // Only deal with non-null/undefined values
            if ((options = arguments[i]) != null) {
                // Extend the base object
                for (name in options) {
                    if (!options.hasOwnProperty(name)) {
                        continue;
                    }
                    copy = options[name];
                    var mm = undefined, src = undefined;
                    if (currentId && jQuery.isArray(options) && jQuery.isArray(target)) {
                        for (mm = 0; mm < target.length; mm++) {
                            if (currentId && (isSameString(target[mm][currentId], copy[currentId]))) {
                                src = target[mm];
                                break;
                            }
                        }
                    }
                    else {
                        src = target[name];
                    }
    
                    // Prevent never-ending loop
                    if (target === copy) {
                        continue;
                    }
                    targetKey = mm !== undefined ? mm : name;
                    // Recurse if we're merging plain objects or arrays
                    if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
                        if (copyIsArray) {
                            copyIsArray = false;
                            clone = src && jQuery.isArray(src) ? src : [];
    
                        }
                        else {
                            clone = src && jQuery.isPlainObject(src) ? src : {};
                        }
    
                        // Never move original objects, clone them
                        if (currentId) {
                            target[targetKey] = merge(deep, clone, copy, currentId);
                        }
                        else {
                            target[targetKey] = merge(deep, clone, copy);
                        }
    
                        // Don't bring in undefined values
                    }
                    else if (copy !== undefined) {
                        target[targetKey] = copy;
                    }
                }
            }
        }
    
        // Return the modified object
        return target;
    };
     function isSameString (a , b){
            return a && b && String(a).toLowerCase() === String(b).toLowerCase();
        }
    obj1 = [ {
        id : "id1",
        name : "name1"
    }, {
        id : "id2",
        name : "name2"
    } ]
    obj2 = [ {
        id : "id3",
        name : "name3"
    }, {
        id : "id1",
        name : "name1_modified"
    } ];
    console.log(merge(true, obj1, obj2, "id"));​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know from here that I can pass an ignore option in the jQuery.validate
Is that possible to know where is a jQuery function launched from ? <div>
I know that jQuery.extend can be used to add by own custom methods to
I know that jQuery, for example, can do animation of sorts. I also know
I know that when linking to libraries such as jQuery that are hosted on
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I know that there are currently questions on SO already pertaining to saving jQuery
I know that the live() event handles event bubbling different than all other jQuery
I am using jquery UI tabs and want to know that , is it
In jquery when I run: $('li').each(some-function(i){}); The IDE seems to know that some-function is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.