I'm having trouble pushing to a multi-dim array, I get the error
_this.fieldRefs[ID].push is not a function
My code is quite simple, so I thought:
this.fieldRefs = this.fieldRefs || []; // init `this.fieldRefs` if it doesn't exist
this.fieldRefs.push(ID); // push the `ID` e.g `0`
this.fieldRefs[ID].push(ref); // add the `ref` to `0` (ref is an ele)
The first two lines work correctly, and this.fieldRefs
contains ID
eg:
this.fieldRefs = [0]
But the final line is where the error occurs. If I check this.fieldRefs[ID]
I do indeed get 0
but I cannot push to 0
index
What you want to do is to assign an array of refs related to an ID.
So if ID will always be a number, instead of pushing this value to an existing array, it would be a better idea to initialize an array in the index defined by that ID. Like this:
this.fieldRefs = this.fieldRefs || [];
this.fieldRefs[ID] = [];
this.fieldRefs[ID].push(ref).
What you are doing currently is different, since you are storing the values of ID on consecutive indices in your fieldRefs
array, but the indices don't match the ID values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.