Javascript 根据特定索引将结果筛选到
我不熟悉ES6和ReactJS。我需要一些帮助来过滤数组中的结果,在某种程度上,我可以检查索引是否匹配,然后调用函数createOptions 实际代码:Javascript 根据特定索引将结果筛选到,javascript,arrays,reactjs,filter,ecmascript-6,Javascript,Arrays,Reactjs,Filter,Ecmascript 6,我不熟悉ES6和ReactJS。我需要一些帮助来过滤数组中的结果,在某种程度上,我可以检查索引是否匹配,然后调用函数createOptions 实际代码: const newArr = items .filter(this.isEligible(selectedIndex)) .filter((item, index) => this.createOptions(item, index, selectedItem)); 需要类似于预期的东西: const newArr =
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));
在这里,当索引等于selectedIndex或selectedIndex+2时,我需要过滤掉结果,然后调用CreateOptions项目、索引、selectedItem
但是,在尝试这样做时,我遇到了一些语法错误。
你能帮我修一下吗 若要访问数组中特定索引处的项,则不需要筛选该数组。只需通过括号符号访问它:
const itemAtIndex = items[selectedIndex]
const itemAtIndexPlusTwo = items[selectedIndex + 2]
你能展示一下你开始时使用的数组和你想结束时使用的数组newArr吗?这样我们就可以看到需要做什么,而不是从你发布的“预期”代码和描述中破译意图。但是,我在尝试这样做时遇到了一些语法错误。那么错误是什么呢?SyntaxError:意外标记,预期:32:209您使用的是三元操作,而没有添加它的else部分。就像,a=条件?b:c。如果selectedIndex | | selectedIndex+2条件不满足,您尚未添加要返回的值。这并不能解决问题。这是一个语法错误。@不可纠正1它可能解决了这个问题。OP显然在努力使用javascript语法,我的最佳猜测是他想访问数组中的项