0

I want to convert string to object's key

I know this logic

const object = { name : 'test' }
const string = name
object[string] = name

I have something problems about this.

const string = common.device.type.pc.name

object[string] <- this is not working

kkangil
  • 33
  • 3

1 Answers1

0

You can do this by split string to each value values = string.split("."); object[values[0]][values[1]][values[2]][values[3]][values[4]] Example: object = {value1: {value2: "123"}}; string = "value1.value2"; values = string.split("."); object[values[0]][values[1]] //return "123"

Demon Spear
  • 104
  • 4