我有一个对象jsonRes[0]
,其中包含需要根据条件删除的值。以下内容可用于删除字符串化对象中的null
、缺失值和等于零的值:
function replacer(key, value) {
// Filtering out properties
if (value === null || value === 0 || value === "") {
return undefined;
}
return value;
}
JSON.stringify(jsonRes[0], replacer, "\t")
然而,当我使用 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
为什么会出现这种情况?有没有解决办法?
value
是一个数字、非数组对象或布尔值,它就没有.includes
方法?你可能是想写类似于typeof value == "string" && value.includes("$")
这样的代码。 - Bergi