如何使用参数对象从数组中删除元素-我的代码有什么问题
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"answers"3",我希望它们被删除。所以调用函数应该返回[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);
工作(它返回[1,1]),但不为destroyer([2, 3, 2, 3], 2, 3);
(它应该返回[],但返回[3])。
请告诉我为什么这不起作用。
我创建了一个repl。它在https://repl.it/BTu5
前面的答案是正确的,但我找到了一些有用的东西,它更类似于问题的代码,帮助他理解:代码:
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);
也为你创作了一个剧本
https://repl.it/BTu5/5示例:slice(1,4)提取第二个元素到第四个元素(索引为1、2和3的元素)
在循环时更新集合是不好的编程实践:对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]);
}
}
}
为什么不直接使用。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);
我在freeCodeCamp上使用参数对象和Array.prototype.filter()完成了这个基本算法脚本挑战。希望能有所帮助
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);
function destroyer(arr) {
// Make a copy of arr by using .slice() and an empty arr2 to put the elements that you want to remove
let arr1 = arr.slice()
let arr2 = [];
// here just iterate starting from 1 because 0 is the array that you want to modifie and push() to arr2
for (let i = 1; arguments.length > i; i++) {
arr2.push(arguments[i]);
}
// And here just filter arr1 with arr2, and that basically removes all arr1 elements that are present in arr2
return arr1.filter(elem => !arr2.includes(elem));
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 验证我的代码json
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- 我不知道为什么我的代码是错误的?又有什么错
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- I'我不知道为什么我的代码没有'不起作用
- 为什么动态加载的事件在我的代码中不能正常工作
- 如何禁用我的切换按钮?这是我的代码
- 我需要将条形图集成到我的代码中
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- 我的代码在<脚本>标记,但没有'不能在外部文件中工作
- 在我的代码中,实体类"卡片展示”;不起作用
- Webrtc和socket.io:createanswer()总是有一个错误.为什么?我的代码连接了两个对等体,但其中只
- 我的代码很完美,但我现在不知道为什么我发现了问题
- Html、css和jQuery.我的代码有问题
- 鼠标事件,我的代码出了什么问题
- 我怎样才能修复我的代码,以便<罢工>不会使我的 Javascript 代码无效
- 如何在我的“代码块”中使表格 Squarespace 网站响应式
- 如何使我的代码更快
- 在我的代码中管理大量硬编码数据的最佳方法