Modifying jQuery extend to push array items within objects but extend other objects

后端 未结 4 1028
太阳男子
太阳男子 2021-01-12 10:55

I\'m thinking this must be a common problem but can\'t seem to find the solution. Using JSON config files to extend a jQuery object that contains objects and arrays.

<
4条回答
  •  情书的邮戳
    2021-01-12 11:12

    I know this is old, but I found this post and used Steve Blacks code above but found a few bugs :

    1. Theres an extra '}' before the 'continue' in the isArray section.
    2. If the source did not have the array at all, it would throw an error, so I added this into the isArray section

      if ( !dst[p] ) {
          dst[p] = src[p];
          continue;
      }
      

    So the finished code looks like this :

    function isDOMNode(v) {
        if ( v===null ) return false;
        if ( typeof v!=='object' ) return false;
        if ( !('nodeName' in v) ) return false; 
        var nn = v.nodeName;
        try {
          v.nodeName = 'is readonly?';
        } catch (e) {
          return true;
        }
        if ( v.nodeName===nn ) return true;
        v.nodeName = nn;
        return false;
    }
    
    function mergeRecursive() {
        // _mergeRecursive does the actual job with two arguments.
        var _mergeRecursive = function (dst, src) {
            if ( isDOMNode(src) || typeof src!=='object' || src===null) {
                return dst; 
            }
    
            for ( var p in src ) {
                if ($.isArray(src[p])) {
                    if ( !dst[p] ) {
                        dst[p] = src[p];
                        continue;
                    }
                    $.merge(dst[p],src[p]);
                    var dupes = {}, singles = [];
                    $.each( dst[p], function(i, el) {
                        if ((dupes[el.name] > -1) &&  (el.name)) {
                            $.extend(singles[dupes[el.name]],el);
                        } else {
                            if (el.name) {
                                dupes[el.name] = i;
                            }
                         singles.push(el);
                       }
                    });
                    dst[p] = singles;
                    continue;        
                }
    
                if ( !src.hasOwnProperty(p) ) continue;
                if ( src[p]===undefined )     continue;
                if ( typeof src[p]!=='object' || src[p]===null) {
                    dst[p] = src[p];
                } else if ( typeof dst[p]!=='object' || dst[p]===null ) {
                    dst[p] = _mergeRecursive(src[p].constructor===Array ? [] : {}, src[p]); 
                } else {              
                    _mergeRecursive(dst[p], src[p]);
                }
            }
            return dst;
        }
    
        // Loop through arguments and merge them into the first argument. 
        var out = arguments[0];
        if ( typeof out!=='object' || out===null) return out;
        for ( var i=1, il=arguments.length; i

提交回复
热议问题