Answer the question
In order to leave comments, you need to log in
How to add to an object a sub-object whose key name will be taken from a variable?
There is an object: let obj = []
. In fact, I declare an array, but it console.log
shows that it is an object.
So, push()
it does not work for him, since it is an object. Adding a new element through obj.key = value
solves the problem, but inserting key
your value from the variable instead of it did not work, and I want something like this:
let obj = []
let href = window.location.href;
obj.href = value;
Answer the question
In order to leave comments, you need to log in
What exactly is happening in your code and what do you plan to achieve with such a crutch?
In your case, you can do:
obj[href] = value
But making an array and calling it an object is wrong. As well as it is wrong to add properties to an array.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question