Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数组中删除随机字符串,JavaScript_Javascript_Arrays_String_Filter_Structure - Fatal编程技术网

从数组中删除随机字符串,JavaScript

从数组中删除随机字符串,JavaScript,javascript,arrays,string,filter,structure,Javascript,Arrays,String,Filter,Structure,我需要从数组中删除字符串,我有这个函数; 它进行一些随机测试并返回结果 function filter_list(array) { array.filter((elem) => typeof elem === "string"); return (array); } 当我不返回任何内容时,我得到的是未定义的(显然),但当我返回数组时,我得到的是: "Expected: '[1, 2]', instead got: '[1, 2, \'a\', \'b\']' Expected: '[

我需要从数组中删除字符串,我有这个函数; 它进行一些随机测试并返回结果

function filter_list(array) {
 array.filter((elem) => typeof elem === "string");
 return (array);
}
当我不返回任何内容时,我得到的是未定义的(显然),但当我返回数组时,我得到的是:

"Expected: '[1, 2]', instead got: '[1, 2, \'a\', \'b\']'
Expected: '[1, 0, 15]', instead got: '[1, \'a\', \'b\', 0, 15]'
Expected: '[1, 2, 123]', instead got: '[1, 2, \'aasf\', \'1\', \'123\', 
123]'
Expected: '[]', instead got: '[\'a\', \'b\', \'1\']'
Expected: '[1, 2]', instead got: '[1, 2, \'a\', \'b\']' " 

你误用了两次时间

第一个问题是,当您调用过滤器时,数组不会改变

// This code isn't the correct yet, continue below
function filter_list(array) {
  // You have to return the result of filter. The 'array' is not changed.
  return array.filter((elem) => typeof elem === "string");
}
第二个问题是,您正在筛选要筛选的相反对象

// Correct code
function filter_list(array) {
  // If the condition is true, the element will be kept in the NEW array.
  // So it must be false for strings
  return array.filter((elem) => typeof elem !== "string");
}
filter()
为中的每个元素调用一次提供的
回调
函数 一个数组,并为其构造一个包含所有值的新数组
callback
返回强制为
true
的值<调用代码>回调 仅适用于具有赋值的数组索引;事实并非如此 为已删除或从未删除的索引调用 指定值。未通过
回调测试的数组元素
只是跳过,不包括在新数组中


你误用了两次时间

第一个问题是,当您调用过滤器时,数组不会改变

// This code isn't the correct yet, continue below
function filter_list(array) {
  // You have to return the result of filter. The 'array' is not changed.
  return array.filter((elem) => typeof elem === "string");
}
第二个问题是,您正在筛选要筛选的相反对象

// Correct code
function filter_list(array) {
  // If the condition is true, the element will be kept in the NEW array.
  // So it must be false for strings
  return array.filter((elem) => typeof elem !== "string");
}
filter()
为中的每个元素调用一次提供的
回调
函数 一个数组,并为其构造一个包含所有值的新数组
callback
返回强制为
true
的值<调用代码>回调
仅适用于具有赋值的数组索引;事实并非如此 为已删除或从未删除的索引调用 指定值。未通过
回调测试的数组元素
只是跳过,不包括在新数组中


不过这很容易。以下是你将如何做到这一点

let数据=[
“猫”,
1451,
14.52,
是的,
“我也将被免职:(”
];
让filteredData=data.filter(item=>typeof item!==“string”);

console.log(filteredData);//或者返回它
这很容易。下面是您将如何执行它

let数据=[
“猫”,
1451,
14.52,
是的,
“我也将被免职:(”
];
让filteredData=data.filter(item=>typeof item!==“string”);

console.log(filteredData);//或返回它
如果您的数字不包含+/-无穷大,则可以使用
array.filter(Number.isFinite)
。如果您的数字都是整数,则使用
array.filter(Number.isInteger)
。如果您的数字不包含+/-无穷大,则可以使用
array.filter(Number.isFinite)
。如果您的数字都是整数,请使用
array.filter(Number.isInteger)
。我明白了!非常感谢您的帮助!!我明白了!非常感谢您的帮助!!