I am using ko.utils.arrayForEach
as mentioned below.
ko.utils.arrayForEach(comments , function(comment) {
tmp.push(comment);
});
Here I am getting all the results and they are pushed to tmp
. If I want to access the first record alone, how can I modify the above code for retrieving the index.
Since version 3.1.0 released on 14 May 2014, index
is passed to all the array functions as the second argument:
ko.utils.arrayForEach(items, function(item, index) {
/* ... */
});
Unfortunately, it's not possible yet. PimTerry added this functionnality on December (see this commit), but it has not be release yet.
Until now; you can do it manually:
for (var i = 0, j = comments.length; i < j; i++) {
// use an anonymous function to keep the same code structure
(function(comment, i) {
tmp.push(comment);
// do what you need with i here
})(comments[i], i);
}
It's exaclty the code used inside ko.utils.arrayForEach
. The migration will be very easy once Knockout will be released
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With