Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

remove falsy value javascript

let myArray = ['apple', 'orange', 'pair', , 'peach', , undefined, false];

// 1 - Remove all
console.log(myArray.filter(Boolean));
// ["apple","orange","pair","peach"]
console.log(myArray.filter(n => n));
// ["apple","orange","pair","peach"]

// 2 - Remove only empty slots (blank values)
console.log(myArray.filter(String));
/* ["apple","orange","pair","peach",undefined,false]
- if the array is composed by strings only
*/
console.log(myArray.flat());
/* ["apple","orange","pair","peach",undefined,false]
- not recommended as it can inadvertently flatten multi-dimensional arrays
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native nested screens 
Javascript :: p5.js style 
Javascript :: js cant find element 
Javascript :: adding donet chart text center in react 
Javascript :: how to connect mongoose database with nodejs 
Javascript :: minecraft infinite snapshot dimensions 
Javascript :: framer motion styled components 
Javascript :: get the middle character js 
Javascript :: how to execute javascript cde on window rotate 
Javascript :: MongoParseError: option usecreateindex is not supported 
Javascript :: jquery add html to end of div 
Javascript :: how to do a classname variable and string react 
Javascript :: exit program js 
Javascript :: React modal input field auto focus antd 
Javascript :: Add Tailwind CSS to Svelte 
Javascript :: jQuery search immediate children 
Javascript :: loop through json object javascript 
Javascript :: discord.js bot activity 
Javascript :: javascript create matrix 
Javascript :: javascript division get remainder 
Javascript :: js vanilla when i remove one object it removes all of them 
Javascript :: vscode default indent type 
Javascript :: geolocation in javascript 
Javascript :: scrollview react native 
Javascript :: reactjs firebase where map value 
Javascript :: update node js mac to latest version 
Javascript :: discord.js leave guild 
Javascript :: angular string to number 
Javascript :: express draft 
Javascript :: angular httpclient query params not working 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =