Reputation: 53
I have an object in my JS sliced from an array:
self.deploysToDevId = ko.computed(function() {
var deployToDev = ko.utils.arrayFilter(self.builds(), function(build) {
return build.buildType() == 'Deploy to Development';
}, self);
var deployToDevID = deployToDev.slice(0,1).id
return deployToDevID;
});
This object is originally from an array called builds and is a build object. However, I am trying to get a property, the "id" of the build which is a property of build, and I want to be able to store this and return this as an object, however when I do the data-bind prints the text [Object object]
self.getIdOfMostRecentDeploy = ko.computed(function() {
var idOfMostRecentDeploy = (self.deploysToDevId().id);
return idOfMostRecentDeploy;
});
This should store the id of the object as "idOfMostRecentDeploy" but it is not doing so.
self.deploysToDevId = ko.computed(function() {
var deployToDev = ko.utils.arrayFilter(self.builds(), function(build) {
return build.buildType() == 'Deploy to Development';
},
self);
var deployToDevID = deployToDev[0];
return deployToDevID;
});
This is another way I have tried, this time trying to take the first object in the array and assign its id to a variable. although, I still just get [Object object]
Upvotes: 1
Views: 1627
Reputation: 1117
when you do this:
self.deploysToDevId = ko.computed(function() {
var deployToDev = ko.utils.arrayFilter(self.builds(), function(build) {
return build.buildType() == 'Deploy to Development';
}, self);
var deployToDevID = deployToDev.slice(0,1).id
return deployToDevID;
});
you are creating a computed that will contain just the id
of the deployToDev
object, which I presume is an observable (that is, a javascript function you need to eval to get the value)
Then, when you try to access it using:
(self.deploysToDevId().id)
this is returning basically nothing, because the "id" you are looking for is in the computed itself, self.deploysToDevId()
, not in any inner id property
EDIT:
I have created this fiddle that may help you:
https://jsfiddle.net/r88zkn11/
in you case the only difference is that the object instead if having id
and buildType
as string objects, they are functions, so if you have to use them you have to evaluate them (use buildType()
instead of buildType
)
Upvotes: 1
Reputation: 43881
When you do this:
var deployToDevID = deployToDev.slice(0,1).id;
You are saying: give me an array one element long, and take the id
property of that array. The array doesn't have an id
property. Instead, you should be indexing the first element:
var deployToDevID = deployToDev[0].id;
Then, as JulioCT notes, the computed will return the id. It doesn't have an id member, it's just the id.
In your last example, you take the first element but don't take its id
property, so naming it deployToDevID
is misleading. Take the id
of it as I suggest above, and then use deployToDevID
anywhere you want to use that id.
Upvotes: 0