我是ES6和ReactJS的新手。我需要一些帮助来过滤掉数组中的结果,在某种程度上,我可以检查索引是否匹配,然后才调用函数createOptions()
.
实际代码:
const newArr = items
.filter(this.isEligible(selectedIndex))
.filter((item, index) => this.createOptions(item, index, selectedItem));
需要类似(预期(的东西(:
const newArr = items
.filter(this.isEligible(selectedIndex))
.filter((item, selectedIndex) => selectedIndex || selectedIndex+ 2 ? this.createOptions(item, selectedIndex, selectedItem));
在这里,我需要过滤掉index
等于selectedIndex
或selectedIndex+2
时的结果,然后调用createOptions(item, index, selectedItem);
但是,我在尝试这样做时遇到了一些语法错误。 你能帮我解决这个问题吗?
如果要访问数组中特定索引处的项目,则无需过滤该数组。只需通过括号符号访问它:
const itemAtIndex = items[selectedIndex]
const itemAtIndexPlusTwo = items[selectedIndex + 2]