I have variables assigned to values as:
let x = 5;
let y = 17;
let z = 0;
const newArr = [];
Here I am trying to push the x, y, x
values if they aren't falsy values
I am doing as:
if(x){
newArr.push(x);
}
if(y){
newArr.push(y);
}
if(z){
newArr.push(z);
}
May I know more efficient way to do this, TIA
You might put all the variables into another array, then filter that array:
let x = 5; let y = 17; let z = 0; const newArr = [x, y, z].filter(Boolean); console.log(newArr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.