为什么控制台会告诉我.filter不是函数? [英] Why does console tell me that .filter is not a function?

查看:57
本文介绍了为什么控制台会告诉我.filter不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var str = "I am a string.";

console.log(str.split(''));

var fil = function(val){
return val !== "a";
};

console.log(str.filter(fil));

运行此命令时,它说str.filter不是函数.

When I run this, it says the str.filter is not a function.

推荐答案

因为 filter 是一个数组函数( Array.prototype.filter ),而您正在调用它在一个字符串上. str.split 返回一个数组,并且不会对您的 str 进行任何更改.像 console.log(str.split('').filter(fil))这样命名,应该没问题.

Because filter is an array function (Array.prototype.filter), while you are calling it on a string. str.split returns an array and doesn't change anything to your str. call it like console.log(str.split('').filter(fil)) and it should be fine.

这篇关于为什么控制台会告诉我.filter不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆