Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

array filter falsy values

const numbersWithFalsyValues = [7, null, 10, 17, false, NaN];
const numbers = numbersWithFalsyValues.filter(Boolean);
console.log(numbers); //[7,10,17]

const namesWithFalsyValues=["Raj", "", "Joy", undefined, false];
const names = namesWithFalsyValues.filter(Boolean);
console.log(names); //['Raj','Joy']

const mixDataWithFalsyValues = [2, 0, "", "Joy", null, undefined, false];
const mixData = mixDataWithFalsyValues.filter(Boolean);
console.log(mixData); //[2,'Joy']
Comment

filter the falsy values out of an array in a very simple way!

var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ];
 
var filtered = arr.filter(Boolean);
console.log(filtered);
 
/*
    Output: [ 1, 2, 3 ]
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get max value from array of objects in javascript 
Javascript :: https request node.js output incomplete 
Javascript :: javascript find prototype 
Javascript :: chaine de caractère dans une autres js 
Javascript :: How to concatenate two textbox values in JavaScript 
Javascript :: deploy react to azure : web.config 
Javascript :: Expo Location get getCurrentPositionAsync not returning anything 
Javascript :: confirm closing tab 
Javascript :: callback hell 
Javascript :: javascript non primitive data types 
Javascript :: mongoose create populate response 
Javascript :: javascript this Inside Constructor Function 
Javascript :: useQuery apollo more than one 
Javascript :: VS Code Auto Import is bugging usestate 
Javascript :: javascript object array sum of values in object 
Javascript :: socket io express 
Javascript :: params scope in javascript 
Javascript :: play a sound in js 
Javascript :: jquery clone table row 
Javascript :: expected an identifier and instead saw const 
Javascript :: datepicker auto select off 
Javascript :: java script to send email 
Javascript :: javascript check string empty 
Javascript :: javascript copy object except one property 
Javascript :: react native full screen view video player 
Javascript :: decapitalize javascript string 
Javascript :: javascript online compiler 
Javascript :: javascript set() handler 
Javascript :: leafletjs code 
Javascript :: convert matrix string to matrix javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =