简体   繁体   English

在函数中使用 .includes 方法

[英]Using .includes method in a function

I have a an object jsonRes[0] containing values which need to be removed based on a condition.我有一个对象jsonRes[0]包含需要根据条件删除的值。 The following works to remove null , missing values and those equal to zero in the stringified object:以下工作用于删除字符串化对象中的null 、缺失值和那些等于 0 的值:

function replacer(key, value) {
          // Filtering out properties
          if (value === null || value === 0 || value === "") {
            return undefined;
          }
          return value;
        } 

JSON.stringify(jsonRes[0], replacer, "\t")

However, when I add a condition using the the includes method, I receive an error:但是,当我使用includes方法添加条件时,我收到一个错误:

function replacer(key, value) {
          // Filtering out properties
          if (value === null || value === 0 || value === "" || value.includes("$")) {
            return undefined;
          }
          return value;
        } 


Uncaught TypeError: value.includes is not a function

Why is this the case and is there a workaround?为什么会这样,是否有解决方法?

You can use String.indexOf() instead of String.includes , As it is available in ES6 and not supported in IE at all.您可以使用String.indexOf()而不是String.includes ,因为它在 ES6 中可用,而在 IE 中根本不支持。

typeof value == "string" && value.indexOf('$') > -1

Also note if value is not string type it will still raise an error boolean , Number doesn't the the method.另请注意,如果value不是 string 类型,它仍会引发错误booleanNumber不是该方法。 You can use typeof to validate whether value is a string.您可以使用typeof来验证value是否为字符串。

The .includes() API is part of the String and Array data type. .includes() API 是StringArray数据类型的一部分。

So what the error is trying to tell you is that the value for variable value , eg an integer or object, does not have the property .includes .所以错误试图告诉你的是变量value ,例如整数或对象,没有属性.includes

You could do checks like你可以做检查

  1. typeof a_string === 'string'
  2. an_array instanceof Array

before the .includes() api to prevent this..includes() api 之前防止这种情况。

Obviously this will make your if statement rather ugly due to the number of checks you have.显然,由于您拥有的检查数量,这会使您的 if 语句变得相当难看。

Based on the way your code is written I suspect you are more interested in checking "String" than array.根据您编写代码的方式,我怀疑您对检查“字符串”比检查数组更感兴趣。 So becareful of arrays.所以要小心数组。 Your code may not work properly if it is array.如果它是数组,您的代码可能无法正常工作。

Anyway here is a refractored version of your code.无论如何,这是您代码的折射版本。

 function replacer(key, value) { // Filtering out properties if (!value || typeof value === "string" && value.includes("$")) { return undefined; } return value; } console.log("NULL returns:" + replacer('test', null)); console.log("$Test returns:" + replacer('test', '$test')); console.log("Blah returns:" + replacer('test', 'Blah'));

还有一种可能性:也许您的value不是字符串类型对象。

(typeof(value) == "string" && value.includes("$"))

I solved this error, which I was getting when applying "includes" to a "window.location" value, by appending ".toString();"我通过附加“.toString();”解决了这个错误,这是我在将“includes”应用到“window.location”值时遇到的错误。

var requestUrl = window.location.toString(); var requestUrl = window.location.toString();

if (requestUrl.includes(urlBase + "#")) { ... if (requestUrl.includes(urlBase + "#")) { ...

I actually am not sure what type of the variable named value is, but anyway, Array.prototype.includes and String.prototype.includes are only available in ES6.我实际上不确定名为value的变量是什么类型,但无论如何, Array.prototype.includesString.prototype.includes仅在 ES6 中可用。 You need to use babel-polyfill or any other bundling modules like rollup.js , webpack with babel or something like that to use includes function.您需要使用babel-polyfill或任何其他捆绑模块(如rollup.js 、带有 babel 的webpack或类似的东西)才能使用includes功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM