0

I have an object in my code, and I want to get the properties of the object, that have the name of a seperat variable.

const object = {
    first: 1,
    second: 2,
    third: 3
};

let input = first;
let result;

setInterval(() => {
    result = object.input; //Here
    console.log(result);
}, 50);

On the line with the //Here, I want to get the property of the object with the name of the input. The input's value changes over time, depending on the user, that's why I can't hard code it in.

Thanks in advance.

Tobias H.
  • 426
  • 1
  • 6
  • 18

1 Answers1

1

you can use [] instead of .

as here

const object = {
    first: 1,
    second: 2,
    third: 3
};

let input = 'first';
let result;

setInterval(() => {
    result = object[input]; 
    console.log(result);
}, 50);
Adnan Tariq
  • 167
  • 5