Initialize an object with a key at runtime

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Often I want to construct an object with a runtime-generated key.



Here's how I have been doing it:



function make_obj(key, func, val)
let foo=;
foo[key] = func(val);
return foo;



Usage:



make_obj('food', x=>`I like $x`, 'soup'); // food: 'I like soup'


Is there a shorter way to do this?







share|improve this question















  • 1




    You can use [key]: func(val) to create an object literal with a dynamically keyed property.
    – Aluan Haddad
    Jan 14 at 2:09










  • @AluanHaddad that's perfect, if you make it an answer I'll accept it.
    – hoosierEE
    Jan 14 at 2:12
















up vote
0
down vote

favorite












Often I want to construct an object with a runtime-generated key.



Here's how I have been doing it:



function make_obj(key, func, val)
let foo=;
foo[key] = func(val);
return foo;



Usage:



make_obj('food', x=>`I like $x`, 'soup'); // food: 'I like soup'


Is there a shorter way to do this?







share|improve this question















  • 1




    You can use [key]: func(val) to create an object literal with a dynamically keyed property.
    – Aluan Haddad
    Jan 14 at 2:09










  • @AluanHaddad that's perfect, if you make it an answer I'll accept it.
    – hoosierEE
    Jan 14 at 2:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Often I want to construct an object with a runtime-generated key.



Here's how I have been doing it:



function make_obj(key, func, val)
let foo=;
foo[key] = func(val);
return foo;



Usage:



make_obj('food', x=>`I like $x`, 'soup'); // food: 'I like soup'


Is there a shorter way to do this?







share|improve this question











Often I want to construct an object with a runtime-generated key.



Here's how I have been doing it:



function make_obj(key, func, val)
let foo=;
foo[key] = func(val);
return foo;



Usage:



make_obj('food', x=>`I like $x`, 'soup'); // food: 'I like soup'


Is there a shorter way to do this?









share|improve this question










share|improve this question




share|improve this question









asked Jan 14 at 2:05









hoosierEE

3251212




3251212







  • 1




    You can use [key]: func(val) to create an object literal with a dynamically keyed property.
    – Aluan Haddad
    Jan 14 at 2:09










  • @AluanHaddad that's perfect, if you make it an answer I'll accept it.
    – hoosierEE
    Jan 14 at 2:12












  • 1




    You can use [key]: func(val) to create an object literal with a dynamically keyed property.
    – Aluan Haddad
    Jan 14 at 2:09










  • @AluanHaddad that's perfect, if you make it an answer I'll accept it.
    – hoosierEE
    Jan 14 at 2:12







1




1




You can use [key]: func(val) to create an object literal with a dynamically keyed property.
– Aluan Haddad
Jan 14 at 2:09




You can use [key]: func(val) to create an object literal with a dynamically keyed property.
– Aluan Haddad
Jan 14 at 2:09












@AluanHaddad that's perfect, if you make it an answer I'll accept it.
– hoosierEE
Jan 14 at 2:12




@AluanHaddad that's perfect, if you make it an answer I'll accept it.
– hoosierEE
Jan 14 at 2:12















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185077%2finitialize-an-object-with-a-key-at-runtime%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185077%2finitialize-an-object-with-a-key-at-runtime%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?