如何在 JavaScript 中删除空白数组

我是 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>"]

如何删除空白数组?我正在尝试为数组推送添加数组并为数组弹出删除数组,但我需要一个没有空白数组的数组。


慕容708150
浏览 119回答 4
4回答

慕仙森

最后做: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>"]]

慕盖茨4494581

要从数组中删除空值,您需要使用filter函数:var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];var filtered = array.filter(function (el) {&nbsp; return el != "";});console.log(filtered);

皈依舞

你只需使用filterlet 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);

胡子哥哥

通过使用( to ).filter的隐式强制,您可以实现这一点""falsebaseArray.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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript