Skip to content

Instantly share code, notes, and snippets.

@MSerj
Last active March 16, 2020 08:08
Show Gist options
  • Save MSerj/2569881c80ef94977f5ca8b21947fdcd to your computer and use it in GitHub Desktop.
Save MSerj/2569881c80ef94977f5ca8b21947fdcd to your computer and use it in GitHub Desktop.
Some js feature
// conditional add prop
const user = { id: 100, name: 'Howard Moon' }
const password = 'Password!'
const userWithPassword = {
...user,
id: 100,
...(password && { password })
}
userWithPassword //=> { id: 100, name: 'Howard Moon', password: 'Password!' }
//reduce
var cars = ['BMW','Benz', 'Benz', 'Tesla', 'BMW', 'Toyota'];
var carsObj = cars.reduce(function (obj, name) {
obj[name] = obj[name] ? ++obj[name] : 1;
return obj;
}, {});
carsObj; // => { BMW: 2, Benz: 2, Tesla: 1, Toyota: 1 }
//clean array from duplicates
let arr = [1, 1, 2, 2, 3, 3];
let deduped = [...new Set(arr)] // [1, 2, 3]
//changing var values
let a = 2;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment