0

I have the following object:

var list = {
    "to do": {
    key: 99, 
    important: ["example1", "example2"], 
    others: ["example3", "example4"]
    }
};

I want to understand how I can extract the value to do from list.

I want to log the value to a variable: var x = "to do"

I tried to locate it, but to do does not seem to have an index. list[0] // undefined.

Does anyone have a simple solution how to do this ?

Jakob QN
  • 21
  • 3

2 Answers2

0

Use Object.keys() to get the keys of list. Since you only have one key use [0] If you have more than one key solution may differ

var list = {
    "to do": {
    key: 99, 
    important: ["example1", "example2"], 
    others: ["example3", "example4"]
    }
};
let x=Object.keys(list)[0];
console.log(x)
depperm
  • 10,606
  • 4
  • 43
  • 67
0

First, extract data from the list variable followed by your key string.

   var list = {
        "to do": {
        key: 99, 
        important: ["example1", "example2"], 
        others: ["example3", "example4"]
        }
    };
    var x=list["to do"];
    console.log(x)
akash
  • 779
  • 5
  • 16