为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 JavaScript 中删除空白数组

如何在 JavaScript 中删除空白数组

慕容708150 2023-11-02 17:15:21
我是 JavaScript 新手。初始数据:["Argentina", "Maroon 5:50<br>"]["Australia", "Maroon 5:224<br>"]["Austria", "Maroon 5:42<br>"]["Belgium", "Maroon 5:10<br>"][""]["Brazil", "Maroon 5:187<br>"][""]["Canada", "Maroon 5:160<br>"][""]["China", "Maroon 5:10<br>"][""][""][""][""][""][""][""][""]["France", "Maroon 5:1533<br>"]我的代码:     $.each(resp.result.artists_detail, function(index, item) {  var arr = [];  var pop_val = [];                         var artistdetails="";  for (var i = 0; i < resp.result.artists.length; i++) {    arr.push(item[0].country_name);    if(item[i].play_count != 0){      artistdetails +=item[i].artist_name +':'+ item[i].play_count+'<br>';    }else{      arr.pop(artistdetails);    }    }  arr.push(artistdetails);  arrOfChart.push(arr);});我需要这个结果:["Argentina", "Maroon 5:50<br>"]["Australia", "Maroon 5:224<br>"]["Austria", "Maroon 5:42<br>"]["Belgium", "Maroon 5:10<br>"]["Brazil", "Maroon 5:187<br>"]["Canada", "Maroon 5:160<br>"]["China", "Maroon 5:10<br>"]["France", "Maroon 5:1533<br>"]如何删除空白数组?我正在尝试为数组推送添加数组并为数组弹出删除数组,但我需要一个没有空白数组的数组。
查看完整描述

4 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

最后做:


arrOfChart = arrOfChart.filter(arr=>!(arr.length == 1 && arr[0] == ""))

console.log(arrOfChart)

>>>[

["Argentina", "Maroon 5:50<br>"],

["Australia", "Maroon 5:224<br>"],

["Austria", "Maroon 5:42<br>"],

["Belgium", "Maroon 5:10<br>"],

["Brazil", "Maroon 5:187<br>"],

["Canada", "Maroon 5:160<br>"],

["China", "Maroon 5:10<br>"],

["France", "Maroon 5:1533<br>"]

]


查看完整回答
反对 回复 2023-11-02
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

要从数组中删除空值,您需要使用filter函数:


var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];


var filtered = array.filter(function (el) {

  return el != "";

});


console.log(filtered);


查看完整回答
反对 回复 2023-11-02
?
皈依舞

TA贡献1851条经验 获得超3个赞

你只需使用filter


let data = [["Argentina", "Maroon 5:50<br>"],

["Australia", "Maroon 5:224<br>"],

["Austria", "Maroon 5:42<br>"],

["Belgium", "Maroon 5:10<br>"],

[""],

["Brazil", "Maroon 5:187<br>"],

[""],

["Canada", "Maroon 5:160<br>"],

[""],

["China", "Maroon 5:10<br>"],

[""],

[""],

[""],

[""],

[""],

[""],

[""],

[""],

["France", "Maroon 5:1533<br>"]]


let result = data.filter(([el]) => el);


console.log(result);


查看完整回答
反对 回复 2023-11-02
?
胡子哥哥

TA贡献1825条经验 获得超6个赞

通过使用( to ).filter的隐式强制,您可以实现这一点""false


baseArray.filter( subArray => subArray[0]);

const baseArray = [["Argentina", "Maroon 5:50<br>"],

["Australia", "Maroon 5:224<br>"],

["Austria", "Maroon 5:42<br>"],

["Belgium", "Maroon 5:10<br>"],

[""],

["Brazil", "Maroon 5:187<br>"],

[""],

["Canada", "Maroon 5:160<br>"],

[""],

["China", "Maroon 5:10<br>"],

[""],

[""],

[""],

[""],

[""],

[""],

[""],

[""],

["France", "Maroon 5:1533<br>"]

];


const result = baseArray.filter( subArray => subArray[0]);


console.log(result);


查看完整回答
反对 回复 2023-11-02
  • 4 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信