0

I need to find a report among the reports observable array by Id. How am I supposed to write the underscore statement?

I am using:

_.where(reports(), {id:data.ReportId})

and

_.where(reports(), {id:ko.observable(data.ReportId)})

It always return an empty array.

Then I find out some underscoreKO.js, but it still won't work. Can someone help me? Thanks.


There is another similar post here, but they are not exactly the same. ko.utils.arrayFirst can locate the item, but won't help me update it.


The correct answer came from Daniel A. White, with a little change. Thank you.

One correction:

var record = _.filter(reports(), function (item) { return item.id() == data.ReportId; })
if (record.length > 0) {
    _.first(record).reportStatus("Approved");
}

I find it has to be item.id() instead of just id(). But a million thanks to Daniel!

Community
  • 1
  • 1
Blaise
  • 21,314
  • 28
  • 108
  • 169

2 Answers2

6

I don't think that will work since id in your reports is an observable.

You should use filter.

_.filter(reports(), function(item) {return id() == data.ReportId; })
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

If the id in your reports is observable, you might need to just use filter:

var filteredReports = _.filter(reports(), function (report) {
    return report.id() === data.ReportId;
});
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134