I have declared 2 observable arrays as following in my code
self.versionDataByProduct = ko.observableArray([]);
self.copiedSavedVersionData = ko.observableArray([]);
When i save data i copy all value from self.versionDataByProduct into self.copiedSavedVersionData like below
self.copiedSavedVersionData(self.versionDataByProduct());
Then i try to compare 2 observable array on button event called compareArray like below
this.compareArray = function () {
debugger;
var results = [];
var differences = ko.utils.compareArrays(self.versionDataByProduct(), self.copiedSavedVersionData());
ko.utils.arrayForEach(differences, function (difference) {
if (difference.status === "deleted") {
results.push(difference.value);
}
});
return results;
};
But this doesnt work. Because after i copy one observable array to other and do some modification in observable array then it automatically updates other. I dont want that. I want my observable array self.copiedSavedVersionData to remain as is after it is copied. How can i achieve it? I am just doing it track if i have done any changes. If array is changed then i will enable button in my code. Currently i dont know how do i go about dirty tracking so found this alternative but not working. Please help.
You can use
slicefunction for this:In that case ko will clone array.