数组去除空值的方法:1、使用filter方法,语法“数组.filter(function(元素值,索引值,数组对象), this的值);2、使用splice方法,语法“数组对象.splice(整数,项目数量,向数组添加的新项目)”。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
filter 过滤方法
var arr = ['A', '', 'B', null, undefined, 'C', ' ']; var r = arr.filter(function (s) { return s && s.trim(); // 注:IE9(不包含IE9)以下的版本没有trim()方法 });
splice方法
function trimSpace(array){ for(var i = 0 ;i<array.length><p>【推荐学习:<a href="https://www.php.cn/course/list/2.html" target="_blank">javascript高级教程</a>】</p></array.length>
以上就是javascript中数组如何去掉空值的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号