iskandarblue
iskandarblue

Reputation: 7526

Using .includes method in a function

I have a an object jsonRes[0] containing values which need to be removed based on a condition. The following works to remove null, missing values and those equal to zero in the stringified object:

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:

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?

Upvotes: 31

Views: 155027

Answers (5)

AndrewStein
AndrewStein

Reputation: 101

I solved this error, which I was getting when applying "includes" to a "window.location" value, by appending ".toString();"

var requestUrl = window.location.toString();

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

Upvotes: 1

Samuel Toh
Samuel Toh

Reputation: 19278

The .includes() API is part of the String and Array data type.

So what the error is trying to tell you is that the value for variable value, e.g. an integer or object, does not have the property .includes.

You could do checks like

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

before the .includes() api to prevent this.

Obviously this will make your if statement rather ugly due to the number of checks you have.

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'));

Upvotes: 17

Satpal
Satpal

Reputation: 133403

You can use String.indexOf() instead of String.includes, As it is available in ES6 and not supported in IE at all.

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. You can use typeof to validate whether value is a string.

Upvotes: 46

tsohr
tsohr

Reputation: 915

Just one more possibility: Maybe your value is not a string type object.

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

Upvotes: 6

IzumiSy
IzumiSy

Reputation: 1528

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. 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.

Upvotes: 0

Related Questions