I'm encountering an issue when attempting to utilize the push()
method to add a property to an object named counterType
. The error message I receive is as follows:
Uncaught TypeError: Cannot read property 'push' of undefined
The goal is to extract specific data from a JSON file and incorporate it into the counterType
object. Here is the code snippet in question:
let ret = JSON.parse(this.responseText);
let counterType: any = {};
for (let i = 0; i < ret.topdownGranularities.TOPDOWN_SYSTEM_DATA.rows.length; i++) {
let row = ret.topdownGranularities.TOPDOWN_SYSTEM_DATA.rows[i];
let row_system = row.SYSTEM;
if (row.hasOwnProperty("SYSTEM")) {
counterType[row_system].push({ name: row_system, checked: true });
// counterType[row_system] = "name: " + row_system + ", checked: " + true;
}
}
Although the following line successfully executes, it only appends one property during each iteration, as it simply redefines the value of counterType[row_system]
repeatedly:
// counterType[row_system] = "name: " + row_system + ", checked: " + true;
The objective is to continually add
{ name: row_system, checked: true }
to the object with every iteration.