const names = ['Milan', 'Poudel', 'Soudel', 'Academia']
found = names.filter(function(name) {
return name.includes('ou')
})
found.remove()
console.log(names)发布于 2020-02-21 11:37:15
因为.remove并不是一个真正的数组函数。您可以只使用filter
const names = ['Milan', 'Poudel', 'Soudel', 'Academia']
const filteredNames = names.filter((name) => !name.includes('ou'))
console.log(filteredNames)
发布于 2020-02-21 11:38:42
如果你想用'ou‘删除所有的元素,可以这样做。
let names = ['Milan', 'Poudel', 'Soudel', 'Academia']
names = names.filter(function(name) {
return !(name.includes('ou'));
})
console.log(names);发布于 2020-02-21 11:51:30
.remove()的确切来源是什么?你在使用某种类库吗?这是提供准确答案的当务之急。
为了解释你的过程,以及它的缺陷,这里有一个演练:
// Array of strings is initialized
const names = ['Milan', 'Poudel', 'Soudel', 'Academia']
/**
* Filter the names array and return
* a new array of strings that INCLUDE
* strings with 'ou' which will
* be stored in a global variable 'found'
*/
found = names.filter(function(name) {
return name.includes('ou')
})
// Result ['Poudel', 'Soudel']
console.log(found);
// Exception thrown because Array.remove is not a function
found.remove()
/**
* names is still in-tact and in it's original
* state since you returned the result of
* your names.filter function into a global
* variable called 'found'
*/
console.log(names)
您是否正在尝试创建一个Array.remove函数?也许它看起来像这样:
Array.prototype.remove = function (name) {
return this.filter( e => !e.includes(name));
}
let names = ['Milan', 'Poudel', 'Soudel', 'Academia']
names = names.remove('ou')
console.log(names)
https://stackoverflow.com/questions/60331810
复制相似问题