如何使用参数对象从数组中删除元素——我的代码有什么问题?
How to remove elements from an array using arguments object - what's wrong with my code?
我正在尝试从作为参数传递给函数的数组中删除元素。我试图从数组中删除的元素等于函数的以下参数。像这样: destroyer([1, 2, 3, 1, 2, 3], 2, 3);
所以如果数组(破坏者的第一个参数)包含“2”和“3”,我希望它们被删除。所以调用函数应该 return [1, 1].
function destroyer(arr) {
var args = [];
for(var j = 1; j<arguments.length; j++){
args.push(arguments[j]);
for(var i = 0; i<arr.length; i++){
if(args.indexOf(arr[i]) > -1){
arr.splice(i, 1)
}
}
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我不明白为什么我的代码适用于 destroyer([1, 2, 3, 1, 2, 3], 2, 3);
(它 returns [1, 1])但不适用于 destroyer([2, 3, 2, 3], 2, 3);
(它应该 return [],但 returns [3])。
请帮我理解为什么这不起作用。
创建了一个 repl.it
在遍历集合时更新集合是一种糟糕的编程习惯:对 arr.splice()
的调用可能会更改 arr.length
的值。尝试添加 debugger;
语句以查看程序运行时发生的情况。
这是一个从输入数组中删除不需要的元素的版本
/* Call like this:
* removeValues(list, 2, 3);
* removeValues(list, "foo", 4, "bar");
*/
function removeValues(collection) {
var it = collection.splice(0); // clear + create iterator
var unwanted = Array.prototype.slice.call(arguments, 1);
var i;
for (i = 0; i < it.length; i++) {
if (unwanted.indexOf(it[i]) < 0) {
collection.push(it[i]);
}
}
}
楼主回答的对,
但我有一些工作,它更类似于问题的代码,帮助他理解:
代码:
function destroyer(arr) {
var args = [];
//You want add all the nums you want remove from array, so you start from 1, which means second arg,first arg is the array you want to perform
for(var j = 1; j<arguments.length; j++){
//You store the args to an arg array
args.push(arguments[j]);
//Once you have the arg, you want loop the target array, see if the newly added arg exist in the target array, if it is, then remove it
for(var i = 0; i<arr.length; i++){
//Found it, remove it now! note: your found the index, then you need take it out, check the doc for slice function arr.slice([begin[, end]]) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
if(args.indexOf(arr[i]) > -1){
//key change, remove the element
arr.splice(i, i+1)
}
}
}
return arr;
}
destroyer([2, 3, 2, 3], 2, 3);
`
还有,给你编剧了
示例:slice(1,4) 将第二个元素提取到第四个元素(索引为 1、2 和 3 的元素)。
你为什么不直接使用 .filter()?
function destroyer(arr) {
var badValues = Array.prototype.slice.call(arguments); //Construct an array of arguments
badValues.shift(); //Get rid of the first ("good") array element
return arr.filter(function(x) {return badValues.indexOf(x) == -1;}); //Make "bad" values false and thus — filter them.
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我已经使用过滤函数完成了这个挑战,但建议也使用 'indexOf' 以便将数组中的值与要过滤的值进行比较。试试这个)
````
function destroyer(arr) {
// Remove all the values
var temp = [];
for (var i = 1; i < arguments.length; i++) {
temp.push(arguments[i]);
arr = arguments[0].filter(function(value) {
return ( value !== temp[i - 1]) ;
});
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我使用 Arguments Object and Array.prototype.filter() 在 freeCodeCamp 完成了这个基本算法脚本挑战。希望这有帮助
function destroyer(arr) {
var args = [];
//Creating array of arguments
for(var j=1;j<arguments.length;j++){
args.push(arguments[j]);
}
//Filtering the arguments from array
arr = arr.filter(function(val){
if(args.indexOf(val)==-1)
return true;
return false;
});
return arr;
}
驱逐舰([1, 2, 3, 1, 2, 3], 2, 3);
我正在尝试从作为参数传递给函数的数组中删除元素。我试图从数组中删除的元素等于函数的以下参数。像这样: destroyer([1, 2, 3, 1, 2, 3], 2, 3);
所以如果数组(破坏者的第一个参数)包含“2”和“3”,我希望它们被删除。所以调用函数应该 return [1, 1].
function destroyer(arr) {
var args = [];
for(var j = 1; j<arguments.length; j++){
args.push(arguments[j]);
for(var i = 0; i<arr.length; i++){
if(args.indexOf(arr[i]) > -1){
arr.splice(i, 1)
}
}
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我不明白为什么我的代码适用于 destroyer([1, 2, 3, 1, 2, 3], 2, 3);
(它 returns [1, 1])但不适用于 destroyer([2, 3, 2, 3], 2, 3);
(它应该 return [],但 returns [3])。
请帮我理解为什么这不起作用。
创建了一个 repl.it在遍历集合时更新集合是一种糟糕的编程习惯:对 arr.splice()
的调用可能会更改 arr.length
的值。尝试添加 debugger;
语句以查看程序运行时发生的情况。
这是一个从输入数组中删除不需要的元素的版本
/* Call like this:
* removeValues(list, 2, 3);
* removeValues(list, "foo", 4, "bar");
*/
function removeValues(collection) {
var it = collection.splice(0); // clear + create iterator
var unwanted = Array.prototype.slice.call(arguments, 1);
var i;
for (i = 0; i < it.length; i++) {
if (unwanted.indexOf(it[i]) < 0) {
collection.push(it[i]);
}
}
}
楼主回答的对, 但我有一些工作,它更类似于问题的代码,帮助他理解: 代码:
function destroyer(arr) {
var args = [];
//You want add all the nums you want remove from array, so you start from 1, which means second arg,first arg is the array you want to perform
for(var j = 1; j<arguments.length; j++){
//You store the args to an arg array
args.push(arguments[j]);
//Once you have the arg, you want loop the target array, see if the newly added arg exist in the target array, if it is, then remove it
for(var i = 0; i<arr.length; i++){
//Found it, remove it now! note: your found the index, then you need take it out, check the doc for slice function arr.slice([begin[, end]]) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
if(args.indexOf(arr[i]) > -1){
//key change, remove the element
arr.splice(i, i+1)
}
}
}
return arr;
}
destroyer([2, 3, 2, 3], 2, 3);
`
还有,给你编剧了
示例:slice(1,4) 将第二个元素提取到第四个元素(索引为 1、2 和 3 的元素)。
你为什么不直接使用 .filter()?
function destroyer(arr) {
var badValues = Array.prototype.slice.call(arguments); //Construct an array of arguments
badValues.shift(); //Get rid of the first ("good") array element
return arr.filter(function(x) {return badValues.indexOf(x) == -1;}); //Make "bad" values false and thus — filter them.
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我已经使用过滤函数完成了这个挑战,但建议也使用 'indexOf' 以便将数组中的值与要过滤的值进行比较。试试这个) ````
function destroyer(arr) {
// Remove all the values
var temp = [];
for (var i = 1; i < arguments.length; i++) {
temp.push(arguments[i]);
arr = arguments[0].filter(function(value) {
return ( value !== temp[i - 1]) ;
});
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
我使用 Arguments Object and Array.prototype.filter() 在 freeCodeCamp 完成了这个基本算法脚本挑战。希望这有帮助
function destroyer(arr) {
var args = [];
//Creating array of arguments
for(var j=1;j<arguments.length;j++){
args.push(arguments[j]);
}
//Filtering the arguments from array
arr = arr.filter(function(val){
if(args.indexOf(val)==-1)
return true;
return false;
});
return arr;
}
驱逐舰([1, 2, 3, 1, 2, 3], 2, 3);