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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745396859a4625919.html
评论列表(0条)