如何通过Lodash通过唯一的对象字段合并两个对象数组?

时间:2020-06-15 09:06:36

标签: javascript lodash

如何通过Lodash通过唯一字段合并两个对象数组?
一条小提示:条件字段也是一个对象。

逻辑用例说明:

基本上,我有2个样式数组。一种是基本样式,第二种是外部样式,现在我只是通过简单的逻辑来覆盖基本样式,但是我想将它们合并。

但这并不是那么简单,因为每个数组项都包含 query 字段,该字段基本上是CSS媒体查询表达式(如果重要的话,请使用React Native),并且合并这些数组非常理想。通过查询唯一性。 我的意思是:

  • 如果我们都具有minWidth 200:基本外部->仔细合并这些样式,结果数组应包含带有查询的项目:{ {1}},并且此查询的样式应合并 Basic Outer

  • 的2个样式项
  • 如果只有一个数组(基本或外部)包含某些查询,例如minWith 200,因此我们只需要将此查询作为独立项添加到结果数组即可。

更新:如果两个数组(基本数组和外部数组)都包含相同的查询,则外部数组的值应覆盖基本数组中的现有值。唯一值应该被合并。

例如:

mingWidth: 500

请让我知道是否需要一些其他信息。

看看下面的完整代码示例:

var Basic= [
      {
       { query: { minWidth: 200 }, 
       { style: { foo: 'bar', color: 'red', } }
      }
    ]

var Outer = [
    {
       { query: { minWidth: 200 }, 
       { style: { color: 'gold', } }
    },
    ]

var Result = [
      {
       { query: { minWidth: 200 }, 
       { style: {  foo: 'bar', color: 'gold', } }
    },
]

预期结果

var Basic =  [
            {
              query: {minWidth: 50},
              style: {
                color: 'red',
                mode: {
                  'light': {
                    fontSize: 100
                  },
                  'dark': {
                    fontSize: 200
                  },
                },
              },
            },
            {
              query: {minWidth: 100},
              style: {
                width: 2,
                height: 2,
                color: 'white',
                mode: {
                  'light': {
                    backgroundColor: 'zzz',
                  },
                  'dark': {
                    backgroundColor: 'ZZZ',
                  },
                },
              },
            },
            {
              query: {minWidth: 200},
              style: {
                width: 5,
                height: 5,
                mode: {
                  'light': {
                    backgroundColor: 'ttt',
                  },
                  'dark': {
                    backgroundColor: 'TTT',
                  },
                },
              },
            },
          ]

var Outer =  [
            {
              query: {minWidth: 100},
              style: {
                width: 111,
                height: 111,
                fontSize: 15,
                mode: {
                  'light': {
                    backgroundColor: 'nnn',
                  },
                  'dark': {
                    backgroundColor: 'NNN',
                  },
                },
              },
            },
            {
              query: {minWidth: 200},
              style: {
                width: 333,
                height: 333,
                opacity: 0,
                mode: {
                  'light': {
                    backgroundColor: 'bbb',
                  },
                  'dark': {
                    backgroundColor: 'BBB',
                  },
                },
              },
            },
            {
              query: {minWidth: 900},
              style: {
                width: 10,
                height: 10,
              },
            },
          ]

0 个答案:

没有答案