admin管理员组文章数量:1426950
I am using Lodash _.isEqual to deep-pare a local javascript object with another javascript object retrieved through angular $get.
This code says that the objects are different:
$get({...}, function (data) {
cleanupAngularProps(data);
if (_.isEqual(data, {name: 'Someone'}) {
...
}
});
but chaging it a little as follows it now says that they are equal (as expected):
$get({...}, function (data) {
cleanupAngularProps(data);
if (_.isEqual(JSON.parse(JSON.stringify(data)), {name: 'Someone'}) {
...
}
});
I debugged into the Lodash code and it seems to fail because both objects have different constructors.
How can I solve this without cloning the data?
I am using Lodash _.isEqual to deep-pare a local javascript object with another javascript object retrieved through angular $get.
This code says that the objects are different:
$get({...}, function (data) {
cleanupAngularProps(data);
if (_.isEqual(data, {name: 'Someone'}) {
...
}
});
but chaging it a little as follows it now says that they are equal (as expected):
$get({...}, function (data) {
cleanupAngularProps(data);
if (_.isEqual(JSON.parse(JSON.stringify(data)), {name: 'Someone'}) {
...
}
});
I debugged into the Lodash code and it seems to fail because both objects have different constructors.
How can I solve this without cloning the data?
Share Improve this question asked May 2, 2016 at 19:08 mr.Kamemr.Kame 1934 silver badges12 bronze badges 6- 1 JSON.parse is also going to get rid of functions... – elclanrs Commented May 2, 2016 at 19:12
-
It's possible that original
data
contains functions, but when you doJSON.parse(JSON.stringify(data)
you create the same object, but without functions. Because of that in the first caseisEqual
return false, in the second true. – alexmac Commented May 2, 2016 at 19:12 -
What does
cleanupAngularProps
do? You should be able to get a POJO and the raw JSON from an angularjs $http request which would simplify the parison with another POJO. – Explosion Pills Commented May 2, 2016 at 19:30 - @ExplosionPills It es with $promise and $resolved. – mr.Kame Commented May 2, 2016 at 19:33
- @AlexanderMac I checked and It has no "own" functions. Also deleted all the functions defined within proto but they are still different. This is the condition that fails: https://docs.omniref./js/npm/lodash/0.7.0/symbols/_.isEqual#line=1554 – mr.Kame Commented May 2, 2016 at 19:47
3 Answers
Reset to default 4I know this question is three years old at this point, but I have found what I believe is a more acceptable answer. Simply exclude the prototype parison between the two objects:
var result = _.isEqual(
_.omit(data, ['__proto__']),
_.omit({name: 'Someone'}, ['__proto__'])
);
I am not sure if this is a good practice but I solved it by resetting the prototype, hence the constructor.
$get({...}, function (data) {
cleanupAngularProps(data);
data.__proto__ = Object.prototype;
if (_.isEqual(data, {name: 'Someone'}) {
...
}
});
If you need deep parison and ignore constructors, then you can use this code:
const customizer = (a: any, b: any, key: any) => {
if (_.isObject(a) && _.isObject(b)) {
// @ts-ignore
const aProto = a.__proto__.constructor.name
// @ts-ignore
const bProto = b.__proto__.constructor.name
if (aProto != bProto) {
return _.isEqualWith(_.omit(a, ['__proto__']), _.omit(b, ['__proto__']), customizer)
}
}
return undefined
}
_.isEqualWith({foo:1}, {foo:1}, customizer)
本文标签: javascriptLodash isEqual fails because of constructor defined by angularStack Overflow
版权声明:本文标题:javascript - Lodash isEqual fails because of constructor defined by angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745396858a2656855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论