从 JavaScript 中的数组中移除“0”、“undefined”和空值
要删除“0”、“undefined”以及空值,你需要使用 splice() 概念。假设以下内容是我们的数组 −
var allValues = [10, false,100,150 ,'', undefined, 450,null]
以下是使用 for 循环和 splice() 的完整代码 −
示例
var allValues = [10, false,100,150 ,'', undefined, 450,null]
console.log("Actual Array=");
console.log(allValues);
for (var index = 0; index < allValues.length; index++) {
if (!allValues[index]) {
allValues.splice(index, 1);
index--;
}
}
console.log("After removing false,undefined,null or ''..etc=");
console.log(allValues);要运行上述程序,你需要使用以下命令 −
node fileName.js.
此处,我的文件名是 demo88.js。
输出
这将产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo88.js Actual Array= [ 10, false, 100, 150, '', undefined, 450, null ] After removing false,undefined,null or ''..etc= [ 10, 100, 150, 450 ]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP