修改jQuery扩展以推送对象中的数组项,但扩展其他对象 [英] Modifying jQuery extend to push array items within objects but extend other objects

查看:75
本文介绍了修改jQuery扩展以推送对象中的数组项,但扩展其他对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这一定是一个常见的问题,但似乎无法找到解决方案。
使用JSON配置文件扩展包含对象和数组的jQuery对象。

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.

对于对象和简单属性,我想覆盖(如 extend 很好)。

For the objects and simple properties, I want to overwrite (as extend does nicely).

对于数组,可能有也可能没有现有项目。

For the arrays there may or may not be existing items.

目前数组只会覆盖第一个元素

Currently an array just overwrites the first elements

var sourceObj = {propterty:"change Me",anArray:[{name:"first"},{name:"second"}]},
    configJSON = '{"propterty":"New Val","anArray":[{"name":"third"}]}',
    configObj = JSON.parse(configJSON);

$.extend(true,sourceObj,configObj);

http:// jsfiddle.net/PmuwV/

返回:

{propterty:"New Val" , anArray:[{name:"third"},{name:"second"}}

我可以改为:

{propterty:"New Val",anArray:[{name:"first"},{name:"second"},{name:"third"}]}



<是否还允许更新第一和第二对象?

while ALSO allowing for updating "first" and "second" objects?

"anArray":[{"name":"second","newProp":"add newProp to second"}]

可能/应该 extend 是否可以修改以比较数组项,并根据某些规则或设置属性值(如name)进行扩展或添加?

Could/should extend be modified to compare array items and extend or add based on some rule or set property value such as "name"?

感谢您的任何建议或指示。

Thanks for any advice or pointers.

推荐答案

我使用此解决方案 http://jsfiddle.net/PmuwV/2/
如何动态合并两个JavaScript对象的属性?也来自 JavaScript相当于jQuery的扩展方法

I used this solution http://jsfiddle.net/PmuwV/2/ modified from How can I merge properties of two JavaScript objects dynamically? also from JavaScript equivalent of jQuery's extend method

需要isDOMNode()
我刚刚在数组中添加jquery合并(是的,我也觉得很脏),其中需要在合并后清理重复项。
扩展的Jquery源代码非常相似,但我发现它更具可读性。

requires isDOMNode() I just added in a jquery merge (yes I feel dirty too) on arrays in which duplicates will need to be cleaned up post merge. The Jquery source for extend does something very similar but i found this to be more readable.

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 ) {

//my added bit here - [SB]
      if ($.isArray(src[p])){
          $.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;        
      }
//the rest is original - [SB]

      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<il; i++ ) {
    _mergeRecursive(out, arguments[i]);
  }
  return out;
}

这篇关于修改jQuery扩展以推送对象中的数组项,但扩展其他对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆