Old way:
let defaltPerson = {
name: null,
gender: null,
age: null,
country: null,
username: getId(),
id: getId()
}
Chained assignments way:
let defaltPerson = {}
defaultPerson.name = defaultPerson.gender = defaultPerson.age = defaultPerson.country = null;
defaultPerson.username = defaultPerson.id = getId();
Hacky function way
let person = {
...['name', 'gender', 'age', 'country'].reduce((obj, prop)=>({...obj, [prop]: null}), {}),
...['username', 'id'].reduce((obj, prop)=>({...obj, [prop]: getId()}), {}),
}
Hacky function as array prototype way
Array.prototype.ditto = function(v) {
return this.reduce((o, p)=>({...o, [p]: v}), {})
}
let person = {
...['name', 'gender', 'age', 'country'].ditto(null),
...['username', 'id'].ditto(getId())
}
New way:
let defaltPerson = {
name: gender: age: country: null,
username: id: getId()
}
Some methods taken from javascript - Can one set multiple properties inside an object literal to the same value? - Stack Overflow