要检查对象或数组中是否存在空值,请使用include()的概念。
以下是代码-
var nameArray1 = ["John", "David", "Mike", "Sam", null]; if (nameArray1.includes(null) == true) { console.log("array1 contains null value"); } else { console.log("array1 does not contains null value"); } var nameArray2 = ["Adam", "Bob", "Robert"]; if (nameArray2.includes(null) == true) { console.log("array2 contains null value"); } else { console.log("array2 does not contains null value"); } var objectData = { name: null }; if (Object.values(objectData).includes(null)) { console.log("object contains null value"); } else { console.log("object does not contains null value"); }
要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo314.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo314.js array1 contains null value array2 does not contains null value object contains null value