【问题标题】:How can I use lodash merge to combine properties that have different casing如何使用 lodash merge 组合具有不同大小写的属性
【发布时间】:2016-08-04 17:43:15
【问题描述】:

您好,所以我目前的合并问题是我的数据库有两个变量,遗憾的是我无法手动更改变量的名称以相互匹配。因此,我想知道是否可以以某种方式修改 jquery 的合并以组合两个对象并将变量添加在一起而忽略大小写。

如:

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;

  };

感谢大家花时间和我一起研究这个问题!

【问题讨论】:

  • 您是否总是处理两个结果 - 例如“A”和“a”或“B”和“b”?
  • 您是否希望密钥始终为小写,这是否重要,或者您是否需要保留原始对象的大小写?
  • @tsturzl 我需要从原始对象中保留案例,并且不会有多个结果。
  • @abigwonderful 我使用的示例模仿了 lodash api 中的示例,只是将 'a' 大写。因此,可能存在我需要对多个属性执行此操作的情况,前提是它们的名称相同(忽略大小写)。
  • 多个属性是已知的还是动态匹配过程?

标签: javascript jquery lodash


【解决方案1】:

我开始采用这种方式太深入了,但想知道将属性转换为小写的简单函数是否有帮助:

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

【讨论】:

  • 看起来棒极了!我只需要确定是否可以将属性上的大小写更改为全部小写,或者将原始大小写之一保留在其中一个对象上。与此同时,非常感谢你朝着正确的方向推动=D
  • 是的,如果你知道一个总是小写,那么就不需要在两者上运行该函数。只是想确定一下。
  • @BenJohnson 这就是我要建议的,但你说“我需要让这个案例远离原始对象”。如果你想从第一个对象中保留原始案例,你将不得不做更多的事情,并且可能编写自己的 merge 方法。
猜你喜欢
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2016-10-17
  • 1970-01-01
相关资源
最近更新 更多