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.