Ricalyn Haboc
Ricalyn Haboc

Reputation: 75

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: enter image description here

Upvotes: 0

Views: 275

Answers (2)

MKougiouris
MKougiouris

Reputation: 2861

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

Upvotes: 0

Wenushka Dikowita
Wenushka Dikowita

Reputation: 61

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.

Upvotes: 1

Related Questions