Last active
March 25, 2020 20:16
-
-
Save rtorresve/2f03808cc99a111daca6a34233211819 to your computer and use it in GitHub Desktop.
JS concat not empy object values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myObj = { | |
a: 1, | |
b:'foo', | |
c: '', | |
d: null, | |
e: undefined | |
}; | |
const removeEmpty = (obj) => { | |
Object.keys(obj).forEach((key) =>{ | |
if (obj[key] === undefined || obj[key] === '' || obj[key] === null) { | |
delete obj[key]; | |
} else { | |
obj[key] = key + '=' + obj[key]; | |
} | |
}); | |
return obj; | |
}; | |
let aux_str = Object.values(removeEmpty(myObj)).join('&'); | |
console.log('asdf'.concat('&',aux_str)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.