【问题标题】:Concatinating two objects deeply深度连接两个对象
【发布时间】:2022-01-20 07:05:43
【问题描述】:

我有这两个对象:

const tmp = {
  pl: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },

  en: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },
};
const tmp2 = {
  pl: {
    translation: {
      states: {
        foz: { name: 'baz' },
      },
    },
  },

  de: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },
};

如何连接它们? pl 部分很流畅,它可以改变,所以它必须是动态的。

我正在考虑使用 Object.keys 的组合递归地执行此操作,但这似乎有点矫枉过正。

【问题讨论】:

  • 预期输出是什么
  • 如果要克隆的对象只包含简单数据,则可以进行JSON.parse(JSON.stringfy(objToClone)) 操作,即O(n)。

标签: javascript object data-structures


【解决方案1】:

lodash merge 会在这里解决问题:

const tmp = {
  pl: { translation: { states: { foo: { name: 'bar' } } } },
  en: { translation: { states: { foo: { name: 'bar' } } } },
};
const tmp2 = {
  pl: { translation: { states: { foz: { name: 'baz' } } } },
  de: { translation: { states: { foo: { name: 'bar' } } } },
};

console.log(_.merge(tmp, tmp2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

【讨论】:

    【解决方案2】:

    你去吧:

    function merge(o1,o2){
        const result = {}
        for(const key of Object.keys(o1)) result[key] = key in o2 ? merge(o1[key],o2[key]) : o1[key];
        for(const key of Object.keys(o2)) if(!(key in o1)) result[key] = o2[key];
        return result; 
    }
    

    【讨论】:

      【解决方案3】:

      我会建议一个更标准的方法,通用的,尤其是没有库的:

      const extend = (isDeep, objects) => {
      
      // Variables
      let extended = {};
      let deep = isDeep;
      
      // Merge the object into the extended object
      const merge = function (obj) {
          for (let prop in obj) {
              if (obj.hasOwnProperty(prop)) {
                  if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') {
                      // If we're doing a deep merge and the property is an object
                      extended[prop] = extend(deep, [extended[prop], obj[prop]]);
                  } else {
                      // Otherwise, do a regular merge
                      extended[prop] = obj[prop];
                  }
              }
          }
      };
      
      // Loop through each object and conduct a merge
      for (let argument of objects) {
          merge(argument)
      }
      return extended;
      };
      

      您可以简单地调用它来使用它:

      extend(true, [tmp, tmp2])
      

      第一个布尔参数用于执行深度合并或常规合并。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2012-01-24
        相关资源
        最近更新 更多