英文:
Removing array elements that match with arguments and returning a new array
问题
我在传递的数组上使用forEach()。我传递给forEach()的函数是,如果arrays
的元素与给定的某个参数不匹配,则将其推送到newArray
中。
然而,我一直得到一个空数组,而我想看到的是newArray = [1, 2]
。
我在想我的if语句可能有问题,但我真的看不出问题在哪。我似乎无法找到问题所在。
const removeFromArray = function(arrays, ...args) {
const newArray = [];
arrays.forEach((element) => {
if (!args.includes(element)) {
newArray.push(element);
}
});
return newArray;
}
removeFromArray([1,2,3], 3);
英文:
I am using the forEach() on the array that is passed through. The function I am passing into forEach() is if the element of arrays
does not match with one of the arguments given, then push it into the newArray
.
However, I keep getting a blank array, when I want to see newArray = [1, 2]
.
I am thinking there is something wrong with my if statement, but I really don't see what the problem is. I can't seem to put a finger on the issue.
const removeFromArray = function(arrays, ...args) {
const newArray = [];
for (const arg of args) {
if (arrays.forEach((element) => element !== arg))
newArray.push(element);
}
return newArray;
}
removeFromArray([1,2,3], 3);
答案1
得分: 0
尝试使用Array.filter()
方法
const removeFromArray = function(arrays, args) {
return arrays.filter(ele => ele !== args)
}
removeFromArray([1,2,3], 3);
英文:
Try using Array.filter()
method
const removeFromArray = function(arrays, args) {
return arrays.filter(ele => ele !== args)
}
removeFromArray([1,2,3], 3);
答案2
得分: 0
以下是翻译好的部分:
-
I will assume that you are doing this for a learning experience.
我会假设你这样做是为了学习经验。 -
As others have said, filter is what you should be using.
正如其他人所说,你应该使用 filter。 -
With that said you had a couple errors in your code.
话虽如此,你的代码中有一些错误。 -
You did if(array.forEach... which can not be evaluated as its a iterator and is not truthy
你写了 if(array.forEach...),这不能被评估,因为它是一个迭代器并且不是真值。 -
You needed to push the element inside the forEach, not outside if it
你需要在 forEach 内部推送元素,而不是在外部 if 语句。 -
const removeFromArray = function(arrays, ...args) {
const removeFromArray = function(arrays, ...args) {
-
const newArray = [];
const newArray = [];
-
for (const arg of args) {
for (const arg of args) {
-
arrays.forEach(element => {
arrays.forEach(element => {
-
if (element !== arg){
if (element !== arg){
-
newArray.push(element);
newArray.push(element);
-
}
}
-
return newArray;
return newArray;
-
console.log(removeFromArray([1,2,3], 3));
console.log(removeFromArray([1,2,3], 3));
英文:
I will assume that you are doing this for a learning experience. As others have said, filter is what you should be using. With that said you had a couple errors in your code.
- You did if(array.forEach... which can not be evaluated as its a iterator and is not truthy
- You needed to push the element inside the forEach, not outside if it
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const removeFromArray = function(arrays, ...args) {
const newArray = [];
for (const arg of args) {
arrays.forEach(element => {
if(element !== arg){
newArray.push(element);
}
})
}
return newArray;
}
console.log(removeFromArray([1,2,3], 3));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论