29

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?

iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • if the value is undefined, means it does'nt has anything like includes function – Jswq Jan 24 '17 at 05:44
  • If you look at the documentation of includes() :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes, most of the browsers don't support this property. http://stackoverflow.com/questions/31340868/includes-not-working-in-all-browsers You can use widely supported indexOf() after converting the property to string using toString(): – S M Jan 24 '17 at 05:45
  • Well, if `value` is a number, (non-array) object or boolean, it won't have an `.includes` method? Presumably you meant to write something like `typeof value == "string" && value.includes("$")`. – Bergi Jan 24 '17 at 05:46

5 Answers5

45

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
16

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'));
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
6

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

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

tsohr
  • 865
  • 2
  • 15
  • 25
1

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 + "#")) { ...

0

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.

IzumiSy
  • 1,508
  • 9
  • 17