Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  193] [ 7]  / answers: 1 / hits: 122238  / 8 Years ago, tue, january 24, 2017, 12:00:00

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?


More From » javascript

 Answers
16

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.


[#59232] Saturday, January 21, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jerome

Total Points: 592
Total Questions: 98
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;