在过滤器数组上切片参数?
Slicing an Argument on Filter Array?
我已经在免费代码训练营上学习这门课程几个小时了,但是,我找到了一个我不理解的解决方案,我试图在每一行上添加评论,以便在我实现和理解它时记录下来以供将来参考我已经理解了一些行,但我无法理解这段代码的某些部分:
function destroyer(arr) {
// let's make the arguments part of the array
var args = Array.prototype.slice.call(arguments); // this would result into [[1, 2, 3, 1, 2, 3], 2, 3]
args.splice(0,1); // now we remove the first argument index on the array so we have 2,3 in this example
// I DO NOT UNDERSTAND THESE CODES BELOW
return arr.filter(function(element) {
return args.indexOf(element) === -1;
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我已经查看了文档,我发现很难理解这个示例中的代码似乎非常不同。非常感谢您的帮助!
Array.slice 是数组 prototype
的一部分;
prototype
方法只能在 类.
的实例上访问
var arr = ['a', 'b', 'c', 'd'];
// [] is JavaScript shorthand for instantiating an Array object.
// this means that you can call:
arr.slice(someArg1);
arry.splice(someArg2);
arr
在你不理解的代码部分指的是传递给destroyer
函数的第一个参数;在本例中,数组 [1, 2, 3, 1, 2, 3]
arr.filter
正在使用 Array.filter
方法创建数组的 "filtered" 版本,其中仅包含那些通过 function(element) { return args.indexOf(element) === -1; }
定义的 "test" 的值
- 该函数使用
Array.indexOf
检查切片 args 数组(您正确识别为等于 [2, 3]
)是否包含给定的 element
。因为 indexOf
returns -1 当找不到元素时,检查该值等同于检查指定元素是否不在数组中
所有这些的结果 - 以及函数 destroy
的 return 值 - 将是数组 [1, 1]
,表示传递给 [= 的数组的过滤版本20=] 包含所有不等于传递给 destroy 的其他值的值。
我已经在免费代码训练营上学习这门课程几个小时了,但是,我找到了一个我不理解的解决方案,我试图在每一行上添加评论,以便在我实现和理解它时记录下来以供将来参考我已经理解了一些行,但我无法理解这段代码的某些部分:
function destroyer(arr) {
// let's make the arguments part of the array
var args = Array.prototype.slice.call(arguments); // this would result into [[1, 2, 3, 1, 2, 3], 2, 3]
args.splice(0,1); // now we remove the first argument index on the array so we have 2,3 in this example
// I DO NOT UNDERSTAND THESE CODES BELOW
return arr.filter(function(element) {
return args.indexOf(element) === -1;
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我已经查看了文档,我发现很难理解这个示例中的代码似乎非常不同。非常感谢您的帮助!
Array.slice 是数组 prototype
的一部分;
prototype
方法只能在 类.
var arr = ['a', 'b', 'c', 'd'];
// [] is JavaScript shorthand for instantiating an Array object.
// this means that you can call:
arr.slice(someArg1);
arry.splice(someArg2);
arr
在你不理解的代码部分指的是传递给destroyer
函数的第一个参数;在本例中,数组[1, 2, 3, 1, 2, 3]
arr.filter
正在使用Array.filter
方法创建数组的 "filtered" 版本,其中仅包含那些通过function(element) { return args.indexOf(element) === -1; }
定义的 "test" 的值- 该函数使用
Array.indexOf
检查切片 args 数组(您正确识别为等于[2, 3]
)是否包含给定的element
。因为indexOf
returns -1 当找不到元素时,检查该值等同于检查指定元素是否不在数组中
所有这些的结果 - 以及函数 destroy
的 return 值 - 将是数组 [1, 1]
,表示传递给 [= 的数组的过滤版本20=] 包含所有不等于传递给 destroy 的其他值的值。