DEV Community

Discussion on: How to conditionally select array or array of objects in Javascript ?

Collapse
 
raulcg profile image
RaulC-G

//hashmap of arrays
var arrayStore = {
array1: [1,2,3],
array2: ["a", "b", "c"],
array3: [{id: 1}, {id: 2}]
};

// you access to arrays with a name as string.
// if name doesn't exixts in the hashmap, assing
// an empty array as default value.
var array = arrayStore[nameOfArray] || [];