英文:
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=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论