简体   繁体   English

KnockoutJS ArrayFirst不能按预期工作

[英]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. 即使调试后,它始终会返回null,即使它建立了一个匹配项。

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 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! 如果这有效,那么问题是a.SKu是可观察的,而您没有对其进行评估! Read my comment on your original question 阅读我对您的原始问题的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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