0

This is a homework assignment.

I'm trying to use the value from one object as a key for another.

for example...

    const myObj = {
    
    key: "myKey"
    
    }
    
    const myObj2 = {
    
     <--- here i need the value from myObj's key field. So, I thought to put something like myObj.key.value but that 
didnt work so I'm at a loss. I'm fairly new to javascript so this is a little over my head.
    
    }
KingUnity
  • 11
  • 1
  • try something like this `myObj2[myObj.key]` – Daniel A. White Jan 26 '21 at 21:11
  • 1
    Does this answer your question? [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – evolutionxbox Jan 26 '21 at 21:11

3 Answers3

0

Try this:

const myObj = { key: "myKey" };

const myObj2 = { [myObj.key]: "123" };

console.log(myObj2);

Or this:

const myObj = { key: "myKey" };

const myObj2 = {};
myObj2[myObj.key] = "123";

console.log(myObj2);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

Maybe you mean: put the values from obj 1 to an array .

let obj = {de: 80, en: 50, it: 60} => [80,50,60]. 

then you need only: Object.keys(obj)

And you will get:

Array(3) [ "de", "en", "it" ]
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
-1

You need to put square brackets around it.

Kaushal Banthia
  • 117
  • 1
  • 3