In this piece of Javascript
creds = cp[0];
data[creds.toString()] = cp[1]; // data is an object
Chrome gives me the error TypeError: Cannot call method toString of undefined on the second line. However, I’ve verified via the debugger that the value of creds is at that point the number 1400.
What’s going on?
You should be very cautious when using
for inloop to array. Use normal loop instead.The array
cplhas not just data but functions, so thirdcpin the loop is function. That’s whycredsturned to undefined.This link has good explanation: Why is using "for…in" with array iteration a bad idea?