Skip to content

Instantly share code, notes, and snippets.

@pajaydev
Last active September 8, 2020 17:56
Show Gist options
  • Save pajaydev/cb421a6285df7ad727baa52c0e402e5e to your computer and use it in GitHub Desktop.
Save pajaydev/cb421a6285df7ad727baa52c0e402e5e to your computer and use it in GitHub Desktop.
JS Snippets
// use reduce function to find sum of all elements in array
const array = [1,2,3,4,5];
const sumValue = array.reduce((sum, value)=> sum + value, 0); // 15
// check all the children is valid or not.
root.children.every(child => !child)
// filtering the values in JSON.stringify.
const person = {
name: "Ajaykumar",
company: "eBay"
}
const output = JSON.stringify(person, (key,value) => {
if(key === "company") return undefined;
return value;
});
console.log(output) // {"name":"Ajaykumar"}
// check if its mac browser
const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment