简体   繁体   中英

KnockoutJS ArrayFirst doesn't work as expected

I have already tried the solutions here:

But nothing works for me. I am checking if an item is already existing in the observable array

ko.utils.arrayForEach(self.Summary(), function (item) {

            var match = ko.utils.arrayFirst(self.filteredSummary(), function (a) {
                return a.Sku == item.Sku()
            });

            if (!match) {
                // Do push
            }
        });

Am I doing something wrong? This always returns null even though when debugged, it founded a match.

I attached the snippet of the values: 在此处输入图片说明

Check the statement,

return item.Sku() === a.Sku()

=== : equal value and equal type, == : equal to,

https://www.w3schools.com/js/js_operators.asp

In your case both the value and the type of the two summary objects must be equal.

Ok, try this one

ko.utils.arrayForEach(self.Summary(), function (item) {

            var match = ko.utils.arrayFirst(self.filteredSummary(), function (a) {
                return a.Sku() == item.Sku();
            });

            if (!match) {
                // Do push
            }
        });

If this workd, the problem was that a.SKu was an observable and you were not evaluating it! Read my comment on your original question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM