I am new to JS so I am trying to use the associative arrays and am getting an error when I run the following JS code.
Uncaught TypeError: Cannot set properties of undefined (setting 'x')
Any assist would be great.
var activeField;
fields = []; // holds all the defined fields
function Field() {
this.polypoints = [{}];
}
var field = new Field();
var Points;
fields.push(field);
activeField = (fields.length) - 1;
Points = fields[activeField].polypoints.length;
Points = Points 1;
fields[activeField].polypoints[Points].x = 1;
fields[activeField].polypoints[Points].y = 1;
console.log("values in array:", activeField, polyPoints, fields);
CodePudding user response:
When you make Points=Points 1;
, Points
is now 1. fields[activeField].polypoints[Points]
is undefined b/c you haven't created an object. The object is at fields[activeField].polypoints[0]
instead. To add object, you have to create.
fields[activeField].polypoints[Points]={x:1,y:1}