Since the keys themselves are objects, we have to get their lengths like this.
This is more about JavaScript in general than Knockout. To get the value of a Knockout observable, you call it like a function. e.g.: sizeDetails()
. In this case it would return the object, and then we can get individual sizes and return them in an array or whatever format suits you.
function getSizeDetails(){
const sizeDetailsObject = self.sizeDetails();
let sizes = [];
for (var key in sizeDetailsObject) {
//check if key exists
if (sizeDetailsObject.hasOwnProperty(key)) {
//since key is sure to be an object, get its length like this:
sizes.push(Object.keys(sizeDetailsObject[key]).length);
}
}
return sizes;
};
You could also create a pure Computed observable, whose value updates automatically when sizeDetails
gets updated.
self.sizes = ko.pureComputed(function(){
const sizeDetailsObject = self.sizeDetails();
let sizes = [];
for (var key in sizeDetailsObject) {
if (sizeDetailsObject.hasOwnProperty(key)) {
sizes.push(Object.keys(sizeDetailsObject[key]).length);
}
}
return sizes;
}, self);
Now you can always get the latest sizes by calling self.sizes()
.
To get their sizes individually, you can do as Erick and Bivo have mentioned in their answers.