2

I'm trying to reduce the hardcoded values in my code. Currently the set up is as follows

//FROM THE DATA FILE
AppleTable = [
{name: 'test', type: 'fruit' },
{name: 'test2', type: 'fruit' },
{name: 'test3', type: 'fruit' },]

BananaTable = [
{name: 'test', type: 'fruit' },
{name: 'test2', type: 'fruit' },
{name: 'test3', type: 'fruit' },]

//IN A DIFFERENT FILE
import {AppleTable, BananaTable} from './Data'

var result = [];
var all = [];
if (category === 'Apples'){
  result = AppleTable.map(value)
  all = Objects.values('AppleTable')
}

But how do I get rid of the hardcoded aspect of it so the information from the AppleTable and BananaTable can be accessed using the 'TableID' in the new Tables object shown below- I have a new Object called Tables and I have tried the following e.g.:

//FROM THE DATA FILE
export const Tables = [
 {name: 'Apple'  , TableID: 'AppleTable'},
 {name: 'Banana' , TableID: 'BananaTable'}
]

export const AppleTable = [
 {name: 'test', type: 'fruit' },
 {name: 'test2', type: 'fruit' },
 {name: 'test3', type: 'fruit' }
]

export const BananaTable = [
 {name: 'test', type: 'fruit' },
 {name: 'test2', type: 'fruit' },
 {name: 'test3', type: 'fruit' }
]

//IN A DIFFERENT FILE
import {Tables} from './Data'

var result = [];
var all = [];

var TableName = '';

for (var i = 0 ; i < Tables.length ; i++){
    if (category === Tables[i].name){
       TableName = Tables[i].TablesID //THIS IS THE NAME OF THE TABLE I WANT
       result = [TableName].map(value) //CARRIED OUT MAPPING USING THAT TABLE 
       all = Objects.values([TableName]) //GET ALL THE VALUES BASED ON THAT TABLE NAME 
   }
} 

I am aware this is not correct - How do I get it so that it maps the data from those tables as opposed to mapping the string name.

Aims.kar
  • 141
  • 1
  • 7
  • Does this answer your question? ["Variable" variables in Javascript?](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) – VLAZ May 29 '20 at 05:40
  • Create an object that holds those arrays then reference them by property name. – VLAZ May 29 '20 at 05:40
  • What are `AppleTable` & `BananaTable` here? arrays? and where are they stored actually? In the `'./Data'` file only? – palaѕн May 29 '20 at 05:47
  • yes those tables are stored in the './Data' file only – Aims.kar May 29 '20 at 05:58

1 Answers1

1

So, instead of setting string, you can simply set the table as reference to the TableID property in main data file like:

//FROM THE DATA FILE
const AppleTable = [
 {name: 'test', type: 'fruit' },
 {name: 'test2', type: 'fruit' },
 {name: 'test3', type: 'fruit' }
]

const BananaTable = [
 {name: 'test', type: 'fruit' },
 {name: 'test2', type: 'fruit' },
 {name: 'test3', type: 'fruit' }
]

export const Tables = [
 {name: 'Apple'  , TableID: AppleTable},
 {name: 'Banana' , TableID: BananaTable}
]

And then in a different file, you can access them like:

if (category === Tables[i].name) {
  TableName = Tables[i].TablesID 
  result = TableName.map(/* your mapping logic here */) 
  all = Object.values(TableName)
}
palaѕн
  • 72,112
  • 17
  • 116
  • 136