Is there a way to make this happen during an object initiation?
//uuid() returns a new uuid
let Obj = {
[uuid()]:{
id: (get the ID that was just created)
}
}
so the output should be something like
Obj {
5cb93583: {
id: 5cb93583
}
}
Is there a way to make this happen during an object initiation?
//uuid() returns a new uuid
let Obj = {
[uuid()]:{
id: (get the ID that was just created)
}
}
so the output should be something like
Obj {
5cb93583: {
id: 5cb93583
}
}
You could use an immediately invoked (arrow) function, together with some other ES6 syntax:
let obj = (id => ({ [id]: {id} }))(uuid());
As a side note: better use camelCase for variable names, and reserve the initial-capital notation only for constructors/classes.
Beside the given IIFE, you could take a explicit function for it, because this approach is reusable.
const
getUUID = _ => Math.floor(Math.random() * (1 << 16)).toString(16),
getObject = id => ({ [id]: { id } });
let object1 = getObject(getUUID()),
object2 = getObject(getUUID());
console.log(object1);
console.log(object2);