I
I
Ivan Ponomarev2021-06-22 20:02:38
JavaScript
Ivan Ponomarev, 2021-06-22 20:02:38

How to add a key to an object when I don't know what it will be called?

how to add a key to an object when I don't know what it will be called. For example, I will iterate over the values ​​from the array and substitute as a key in the object. Or pass key values ​​through a parameter in a function?

const ass = {};

function insert (key,val){
  ass.key = val;
  console.log(ass);
}

insert(22,"Ivan")


Prints key: "Ivan" . How to make it output 22: "Ivan" ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-06-22
@9vanya

ass[key] = val;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question