在这种情况下,如何为lodash _.Mergewith编写定制功能?
我有两个这样的对象:
var obj3 = {
customer_id: 1,
products: { id: 1, name: "Car" },
};
var obj4 = {
customer_id: 1,
products: { id: 2, name: "Dress" },
};
预期的对象是:
result = {
customer_id: 1,
products: [
{ id: 1, name: "Car" },
{ id: 2, name: "Dress" },
],
};
在这种情况下,如何编写function customizer(){}
? 非常感谢您的帮助,我们欢迎以不同的方式提供其他解决方案。
I have two objects like that:
var obj3 = {
customer_id: 1,
products: { id: 1, name: "Car" },
};
var obj4 = {
customer_id: 1,
products: { id: 2, name: "Dress" },
};
and the expected object is:
result = {
customer_id: 1,
products: [
{ id: 1, name: "Car" },
{ id: 2, name: "Dress" },
],
};
How can i write function customizer(){}
in this situation?
Thanks so much for your help, and i'll be welcome for other solution with different way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将对象合并到新对象(
{}
)。将合并值的默认值设置为空数组。如果密钥是产品
concat。如果没有返回未定义
,则将使用merge()
的标准逻辑:Merge the objects to a new object (the
{}
). Set the default values of the merged values to empty arrays. If the key isproducts
concat them. If not returnundefined
, so the standard logic of_.merge()
would be used: