I have two JavaScript objects
in Angularjs
. Both of them having the same keys
but different values
. Which looks like this:
$scope.object1 = {
name: 'Arpit',
roll_no: 999, // Number
creation_date: '2017-04-10T04:44:21.923Z', // date value but string
joining_date: '2017-03-30T18:30:00.000Z' // date value but string
}
and the second object is:
$scope.object2 = {
name: 'Arpit1',
roll_no: '999', // Number but string
creation_date: 2017-04-10T04:44:21.923Z, // date value not string
joining_date: 2017-03-30T18:30:00.000Z // date value not string
}
Now I want to compare both this object and want to detect the different value.
In comparison 999
should be equal to '999'
and '2017-03-30T18:30:00.000Z'
should be equal to 2017-03-30T18:30:00.000Z
and so on.
Challenge
The value of date and number may be in string format so first I need to detect whether the given string is a date or number and then comparison need to be done.
My code
var data = {
options:{
data: $scope.object1
},
newData: $scope.object2
}
angular.forEach(data.options.data, function (value, key) {
//compare fields
if (data.newData[key] != data.options.data[key]) {
if (filedsToSkip.indexOf(key) == -1) {
if ((angular.isDate(data.newData[key]) || angular.isDate(data.options.data[key])) && new Date(data.newData[key]) != new Date(data.options.data[key])) {
$ctrl.updatedFields.push({
field: key,
newValue: data.newData[key],
oldValue: data.options.data[key]
});
}
} else {
console.log("Field skipped");
}
}
});
But this code is not working properly. Please give some suggestion.
Thanks.
EDIT
Right now when I am running this code it is showing that all the fields are different, but I want it to show only name
is different.