【发布时间】:2020-12-26 06:05:15
【问题描述】:
当我运行一个数组时。 filter 函数,它返回所有数组内容而不执行条件检查。请帮助。谢谢
这里是代码
const paragraphFields = ['a', 'b', 'c', 'd', 'ff', 'g'];
const handleDeleteParagraphFields = (array_index) => {
paragraphFields.filter((_, index) => index !== array_index);
console.log(paragraphFields);
return paragraphFields;
};
handleDeleteParagraphFields(2)
这是输出
[
"a",
"b",
"c",
"d",
"ff",
"g"
]
【问题讨论】:
-
你忘了传递参数,所以
array_index是undefined -
@CertainPerformance,我添加参数时是一样的。这是我的帖子中的一个遗漏,我添加了它。请帮助仔细研究问题
-
您应该存储过滤器返回的输出。
const paragraphFields = ['a', 'b', 'c', 'd', 'ff', 'g']; const handleDeleteParagraphFields = (array_index) => { var newArray = paragraphFields.filter((_, index) =>{ return index !== array_index; }); console.log(newArray); return newArray; }; handleDeleteParagraphFields(2) -
如果你只是想从索引中删除一些东西,为什么不使用 array.splice(index, 1) ?
标签: javascript arrays data-structures filter