如何合并包含符号的javascript对象?

And*_*ner 4 javascript merge node.js typescript lodash

我正在尝试使用 lodash merge 合并两个对象,但它不适用于符号。有没有替代的实用程序?

    import {merge} from 'lodash';
    import {Op} from 'sequelize';

    const selectA = {
        where: {
            text: "something"
        }
    };

    const selectB = {
        where: {
            date_from: {
                [Op.lt]: Sequelize.literal('NOW()')
            }
        }
    };

    console.log(_.merge(selectA, selectB));
Run Code Online (Sandbox Code Playgroud)

输出:

{ where: { text: 'something', date_from: {} } }
Run Code Online (Sandbox Code Playgroud)

Ori*_*ori 5

您可以使用_.mergeWith()并提供使用扩展的合并函数。

注意:查看浏览器的控制台。代码段的控制台不显示符号。

const Op = {
  lt: Symbol('symbol')
}

const selectA = {
  num: 15,
  where: {
    text: "something"
  }
};

const selectB = {
  num: 30,
  where: {
    date_from: {
      [Op.lt]: 'symbol value'
    }
  }
};

const result = _.mergeWith(selectA, selectB, (a, b) => {
  if (!_.isObject(b)) return b;
  
  return Array.isArray(a) ? [...a, ...b] : { ...a, ...b }
});

console.log(result);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
Run Code Online (Sandbox Code Playgroud)