我该如何合并2 JavaScript对象,在一个填充属性如果对方不存在吗? [英] How can I merge 2 javascript objects, populating the properties in one if they don't exist in the other?

查看:96
本文介绍了我该如何合并2 JavaScript对象,在一个填充属性如果对方不存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个JavaScript对象/副教授。这样定义的数组:

 函数somefunction(选项){    无功默认值= {
        为prop1:'富',
        prop2:'棒'
    };    //这里做的东西}

和我想用这个作为函数的默认值。因此,当函数被调用我要填充的选项默认,但前提是他们不使用值变量T存在于选项

因此​​,可以说,这种被称为

  somefunction({为prop1:'鱼'});

我怎样才能让这个选项获取与默认合并这样的,我得到这个

  {
    为prop1:鱼,
    prop2:'棒'
}


解决方案

您可以看看无论是jQuery的或原型延长功能。

它看起来像这样:(从jQuery的直接取)

  jQuery.extend = 1.3中=功能(){
    //拷贝参考目标对象
    VAR的目标=参数[0] || {},I = 1,长度=与arguments.length,深=虚假,选择;    //处理深副本的情况
    如果(typeof运算目标===布尔){
    深=目标;
    目标=参数[1] || {};
    //跳过布尔和目标
    我= 2;
    }    //当目标是一个字符串或东西(可能在深副本)手柄的情况下
    如果(typeof运算目标==对象&放大器;!&安培;!jQuery.isFunction(目标))
    TARGET = {};    //本身扩展jQuery的,如果只有一个参数传递
    如果(长度== I){
    目标=这一点;
    \t - 一世;
    }    对于(; I<长度;我+ +)
    //只处理非空/未定义的值
    如果((选项=参数[一])!= NULL)
    //扩展基本对象
    对于(期权变数名称){
    变种SRC =目标[名称],复制=选项[名]    // prevent永无止境的循环
    如果(目标===复印件)
    继续;    //递归如果我们合并对象的值
    如果(深和放大器;&放大器;复制和放大器;&安培; typeof运算副本===对象&放大器;&安培;!copy.nodeType)
    目标[名称] = jQuery.extend(深,
    //不要移动原始对象,他们克隆
    SRC || (copy.length = NULL []:{})
    ,复印件);    //不要在未定义的值不会带来
    否则,如果(副本!==未定义)
    目标[名称] =复印件;    }    //返回修改的对象
    返回的目标;
};

If I have a javascript object/assoc. array defined like this:

function somefunction(options) {

    var defaults = {
        prop1: 'foo',
        prop2: 'bar'
    };

    //Do stuff here

}

and I want to use this as the default values for the function. So when the function gets called I want to populate the options variable with the values in defaults, but only if they don't exist in options.

So lets say that this was called

somefunction({ prop1: 'fish' });

How can I make it so that options gets merged with defaults such that I get this

{
    prop1: 'fish',
    prop2: 'bar'
}

解决方案

You could look at either jQuery's or Prototypes extend functionality.

It looks like this: (taken directly from jQuery)

jQuery.extend = jQuery.fn.extend = function() {
    // copy reference to target object
    var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;

    // 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 ( var name in options ) {
    			var src = target[ name ], copy = options[ name ];

    			// Prevent never-ending loop
    			if ( target === copy )
    				continue;

    			// Recurse if we're merging object values
    			if ( deep && copy && typeof copy === "object" && !copy.nodeType )
    				target[ name ] = jQuery.extend( deep, 
    					// Never move original objects, clone them
    					src || ( copy.length != null ? [ ] : { } )
    				, copy );

    			// Don't bring in undefined values
    			else if ( copy !== undefined )
    				target[ name ] = copy;

    		}

    // Return the modified object
    return target;
};

这篇关于我该如何合并2 JavaScript对象,在一个填充属性如果对方不存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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