0

I want to use the value of a variable to access an object of array.

I want to fill a variable with this name and use the variable to access the object.

var data = [
    {lat_P: 37.4419, lon_P: -122.1419, title: 'location 1'},
    {lat_T: 37.4419, lon_T: -122.1419, title: 'location 2'},
   ]

Example:

let type = "T";
let geometry = "lat_" + type;
console.log(data[x].[geometry]);
abbensid
  • 45
  • 7

1 Answers1

1

You are using data[x].[geometry]. you should use data[x][geometry]

var data = [
    {lat_P: 37.4419, lon_P: -122.1419, title: 'location 1'},
    {lat_T: 37.4419, lon_T: -122.1419, title: 'location 2'},
   ]
let type = "T";
let geometry = "lat_" + type;
console.log(data[1][geometry]);
Tareq
  • 5,283
  • 2
  • 15
  • 18