从嵌套数组中筛选出元素

huangapple go评论75阅读模式
英文:

Filter Out Element from Nested Array

问题

let inputArray = [["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false];
let outputArray = inputArray.filter(item => item !== false);

我有一个嵌套的输入数组,其中包含小数组和false语句,如控制台中所示。如何从输入数组中删除false语句?我尝试使用for循环遍历所有6个元素,检查每个元素是否为(if !==false),然后将其push到一个名为output array的新数组中,但我无法使其工作。请问如何解决这个问题?非常感谢你的帮助!

英文:
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]

let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];

从嵌套数组中筛选出元素

I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated 从嵌套数组中筛选出元素

答案1

得分: 1

Directly use Array#filter:

let input = [["1.81", "2.24"], ["5.62", "6.26"], false, ["2.31", "1.64"], false, false];
let res = input.filter(Boolean);
console.log(res);
英文:

Directly use Array#filter:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let input=[[&quot;1.81&quot;,&quot;2.24&quot;],[&quot;5.62&quot;,&quot;6.26&quot;],false,[&quot;2.31&quot;,&quot;1.64&quot;],false,false]
let res = input.filter(Boolean);
console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 01:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定