Skip to content Skip to sidebar Skip to footer

Knockout Binding - Select First Filled Object

I have the following KnockoutJS code in order to make a databinding for a Addresses Form Editor where I can have 4 address types ('home', 'office', etc). http://jsfiddle.net/mxgFQ/

Solution 1:

Knockout's utility function provides the very convienent arrayFirst function, which makes this simple:

self.selectedAddress = ko.observable(
        ko.utils.arrayFirst(self.addresses(), function(item) {
            return item.address1().length > 0;
        }));

Here is the updated fiddle.

Here is a handy reference for KO's utility functions:

Post a Comment for "Knockout Binding - Select First Filled Object"