Original problem:
I'm trying to create a new object, but the insert is a bit complex:
I am generating an object inside an object.
result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
}
return newObj;
}, {} );
I get this error:
Error in v-on handler: "TypeError: Cannot set property '2017-02-01' of undefined"
on the line:
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
What I tried:
- When changing this row (to an unwanted result, but just for testing) to have only one key object (instead of 2), and it works:
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name] = that.dataResult[dataRow.data_id];
- I tried doing something like this - Adding a temp variable and pushing to it the results, at the beginning i thought i solved the problem, but after reviewing this a few times i noticed that the results are getting duplicated and "cat_id"s are getting duplicated.
result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {
temp[that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].app_name] = temp;
}
return newObj;
}, {} );
temp = {};
Main question:
What is the right way to set an object variable in this way:
objectVariable[step1][step2] = result;
Full code:
let that = this;
let result = null;
let temp = {};
this.activeUsers.forEach( user => {
result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].app_name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
}
return newObj;
}, {} );
if (Object.entries(result).length !== 0) {
that.usersToDatas[user.user_id] = result;
}
temp = {};
});