![](/img/trans.png)
[英]Transfer data from a Javascript object to an array. What's wrong with my array?
[英]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);
因此,如果数组(destroyer的第一个参数)包含“ 2”和“ 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])。
请帮我了解为什么这行不通。
我在https://repl.it/BTu5创建了一个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);
我使用参数Object和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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.