2016-08-04 50 views
1

你好,所以我现在合并的问题是我的数据库有两个变量,我不幸地不能改变变量的名称,以手动匹配对方。因此,我想知道如果我可以以某种方式修改jquery的合并来结合两个对象并将变量添加到一起忽略大小写。我如何使用lodash合并来结合具有不同外壳的属性

如:

var object = { 
'a': [ { 'b':2 }, {'d':4}] 
}; 

var other = { 
'A': [{'c': 3}, {'e':5}] 
}; 

_.merge(object, other); 
// -> {'a': [{'b': 2, 'c':3}, {'d':4, 'e':5}] } 

目前的合并方法在我们的lodash库:

/** 
    * Recursively merges own enumerable properties of the source object(s), that 
    * don't resolve to `undefined` into the destination object. Subsequent sources 
    * will overwrite property assignments of previous sources. If a callback is 
    * provided it will be executed to produce the merged values of the destination 
    * and source properties. If the callback returns `undefined` merging will 
    * be handled by the method instead. The callback is bound to `thisArg` and 
    * invoked with two arguments; (objectValue, sourceValue). 
    * 
    * @static 
    * @memberOf _ 
    * @category Objects 
    * @param {Object} object The destination object. 
    * @param {...Object} [source] The source objects. 
    * @param {Function} [callback] The function to customize merging properties. 
    * @param {*} [thisArg] The `this` binding of `callback`. 
    * @returns {Object} Returns the destination object. 
    * @example 
    * 
    * var names = { 
    * 'characters': [ 
    *  { 'name': 'barney' }, 
    *  { 'name': 'fred' } 
    * ] 
    * }; 
    * 
    * var ages = { 
    * 'characters': [ 
    *  { 'age': 36 }, 
    *  { 'age': 40 } 
    * ] 
    * }; 
    * 
    * _.merge(names, ages); 
    * // => { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] } 
    * 
    * var food = { 
    * 'fruits': ['apple'], 
    * 'vegetables': ['beet'] 
    * }; 
    * 
    * var otherFood = { 
    * 'fruits': ['banana'], 
    * 'vegetables': ['carrot'] 
    * }; 
    * 
    * _.merge(food, otherFood, function(a, b) { 
    * return _.isArray(a) ? a.concat(b) : undefined; 
    * }); 
    * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] } 
    */ 
     var args = arguments, 
     length = 2; 

     if (!isObject(object)) { 
     return object; 
     } 
     // allows working with `_.reduce` and `_.reduceRight` without using 
     // their `index` and `collection` arguments 
     if (typeof args[2] != 'number') { 
     length = args.length; 
     } 
     if (length > 3 && typeof args[length - 2] == 'function') { 
     var callback = baseCreateCallback(args[--length - 1], args[length--], 2); 
     } else if (length > 2 && typeof args[length - 1] == 'function') { 
     callback = args[--length]; 
     } 
     var sources = slice(arguments, 1, length), 
     index = -1, 
     stackA = getArray(), 
     stackB = getArray(); 

     while (++index < length) { 
     baseMerge(object, sources[index], callback, stackA, stackB); 
     } 
     releaseArray(stackA); 
     releaseArray(stackB); 
     return object; 

    }; 

谢谢大家的时间寻找到这跟我来!

+0

将你总是会处理两个结果 - 例如“A”和“A”或'B'和'b'? – abigwonderful

+0

您是否希望密钥始终是小写字母,是否重要,还是需要保持原始对象的情况? – tsturzl

+0

@tsturzl我需要保持从原始对象的情况下,不可能是多个结果。 –

回答

0

我开始服用这种方式太深刻了,但不知道是否该转换特性为小写的简单功能可以帮助:

var obj1 = { 
'a': [ { 'b':2 }, {'d':4}] 
}; 

var obj2 = { 
'A': [{'c': 3}, {'e':5}] 
}; 

function propsToLower(obj){ 
    var newObj ={}; 
    for(var item in obj){ 
     newObj[item.toLowerCase()] = obj[item]; 
    } 
    return newObj; 
} 

var newObj1 = propsToLower(obj1); 
var newObj2 = propsToLower(obj2); 

_.merge(newObj1, newObj2); 
+0

看起来很棒!我只需要知道是否可以将属性中的套管更改为小写,或者将其中一个原始套管保留在其中一个对象上。同时,尽管非常感谢你向正确的方向推进= D –

+0

是的,如果你知道一个将始终是小写,那么不需要在两者上运行该功能。只是想确定。 – abigwonderful

+0

@BenJohnson这是我正在建议的,但你说:“我需要保持原始对象的情况下”。如果你想保留第一个对象的原始案例,你将不得不做更多的事情,并且可能会编写你自己的'merge'方法。 – tsturzl

相关问题