-4

I have a variable that defines the integer.

Example:

var integer = x //11111, 222222, 333333, etc.

Then, I have an object:

Object { 111111: "string1", 222222: "string2", 333333: "string3", etc }

How to get a specific string element based on the integer variable I have?

Example: If Selected Variable 22222 is how to get string2?

JJJ
  • 32,902
  • 20
  • 89
  • 102
kamfulebu
  • 229
  • 1
  • 3
  • 11

3 Answers3

2

You can simply use square bracket notation, as so:

const data = { 111111: "string1", 
              222222: "string2", 
              333333: "string3" };
let x = 111111;
console.log(data[x]);
dashton
  • 2,684
  • 1
  • 18
  • 15
1

You can get it like this

var object = { 111111: "string1", 222222: "string2", 333333: "string3"};
    
var string = object['222222'];

console.log(string)
Shridhar Sharma
  • 2,337
  • 1
  • 9
  • 13
1

Pleaser refer this,

var integerVar = 222222;
var jsonObject = { "111111": "string1", "222222": "string2", "333333": "string3"}
console.log(jsonObject[integerVar]);
Nathash
  • 61
  • 3