I am working with a dataset that includes objects and arrays within an object structure like this:
let productDataObj = {
prodType1: {
keyTermsArr: [],
frequencyObj: {},
ratingObj: {}
},
prodType2: {
keyTermsArr: [],
frequencyObj: {},
ratingObj: {}
}
};
As I am writing code to interact with these nested objects and arrays, I would like to store references to them as variables to avoid repetitive code. For example:
let keyTermsArrRef;
let frequencyRef;
let ratingRef;
for (let i = 0; i < products.length; i++) {
if (prodType[i] == 1) {
keyTermsArrRef = productDataObj.prodType1.keyTermsArr;
frequencyRef = productDataObj.prodType1.frequencyObj;
ratingRef = productDataObj.prodType1.ratingObj;
}
else if (prodType[i] == 2) {
keyTermsArrRef = productDataObj.prodType2.keyTermsArr;
frequencyRef = productDataObj.prodType2.frequencyObj;
ratingRef = productDataObj.prodType2.ratingObj;
}
// Perform operations on keyTermsArrRef, frequencyRef, and ratingRef
The challenge I am facing is that the data is not actually stored in the nested arrays but only in the variables I created to reference them. Is there a way to use variables to point to the 'paths' of these nested arrays and objects?