Q:

KnockoutJS - Assign Observable value to JavaScript variable

I am trying to see if I can assign a JavaScript variable to the value of a value of a KO Observable.   
The reason for this is I want to assign it to a jquery module, as a property.   
I know I can do:
    var viewModel = {
    firstName : ko.observable("Bert"),
    lastName : ko.observable("Smith"),
    pets : ko.observableArray(["Cat", "Dog", "Fish"]),
    type : "Customer"
};
viewModel.hasALotOfPets = ko.computed(function() {
    return this.pets().length > 2
}, viewModel)

var jsonData = ko.toJSON(viewModel);

But, what if I don't want everything that is in the view model?   Can i just only select n number of items? 

A:

There two concepts here:

Retrieving the object from a Knockout observable
Manipulating a JavaScript array

Code:
var start = 1; // index of Dog 
var n = 1; // just one item 
var jsonData = ko.toJSON(viewModel.pets().slice(start, start + n)); 

In Knockout, observables are functions.  To set its value you call it with a parameter.  To get its value, you call it. 
JavaScript arrays have a slice function that can be used to get a smaller array. 
UPDATE based on comments on the question:
If you want to take n fields from viewModel, ko.toJSON will work on a subsetted view model:
var n = 2;
var count = 0;
var subModel = {};
for (var k in viewModel) {
    if (count >= n) break;
    if (viewModel.hasOwnProperty(k)) {
        subModel[k] = viewModel[k];
        count++;
    }
}

var jsonData = ko.toJSON(subModel);

If you want specific fields only, create a subsetted model with those:
var jsonData = ko.toJSON({
    firstName: viewModel.firstName,
    type: viewModel.type
});

