【发布时间】:2021-10-29 07:45:38
【问题描述】:
我将在下面给出一个我正在使用敲除编写的示例,但逻辑使用标准js。
<!-- ko foreach: { data: ourWorks, as: 'item' } -->
<div data-bind="visible: $parent.isVisible(item) "></div>
<!-- /ko -->
tags.name 中只有一个值时的 Js 逻辑
isVisible: function (item) {
let itemTag = item.tags || [];
return !!this.chosenTags().length && this.chosenTags().length > 0 ? this.chosenTags().includes(itemTag.name) : item;
},
但如果有多个值,一切都会停止工作。我试图实现嵌套数组的逻辑,但它不起作用,告诉我如何实现它? enter image description here
isVisible: function (item) {
let itemTag = item.tags || [];
if (Array.isArray(itemTag)) {
let tags = this.chosenTags();
return itemTag.reduce(function(accum, currentVal, currentIndex, tags) {
return tags ? tags.includes(currentVal) : false;
}, this);
}
return !!this.chosenTags().length && this.chosenTags().length > 0 ? this.chosenTags().includes(itemTag.name) : item;
},
【问题讨论】:
-
怎么不工作了?你能详细说明一下吗?
标签: javascript filter knockout.js