Search code examples
knockout.jsdurandal

combine ko observables into a ko observable array


I currently have two observables that I want to merge into an array to send to my backend application for processing, they are:

self.ElementData = ko.observable(localStorage.getItem('ElementDataWidget'));
self.scorecardIDLocalStorage = ko.observable(localStorage.getItem('scorecardId'));

The issue I have is my backend will only accept one parameter, not two. How would I merge these two? I have started with this code, but I am getting confused with it.

self.ElementData = ko.observable(localStorage.getItem('ElementDataWidget'));
self.scorecardIDLocalStorage = ko.observable(localStorage.getItem('scorecardId'));
self.LocalstorageData = ko.computed(function(){
    var tempStorage = [];

    ko.Utils.arrayForEach(self.ElementData(), function(item){
        tempStorage.push({
        })
    })
})

Solution

  • I did not test it but there were at least two things missing:

    self.LocalstorageData = ko.computed(function(){
        var tempStorage = [];
    
        ko.Utils.arrayForEach(self.ElementData(), function(item){
            tempStorage.push({
            })
        })
        return tempStorage // <--- !!
    }, this) // <--- !!