var _array=[];
var someint=44;
var somevalue='a string';
var u=[someint];
_array.unshift(u);
_array[u].unshift('somevalue');//*error
why does this not work?
Uncaught TypeError: Cannot call method 'unshift' of undefined
You've added an element to "_array", and its index will be 0. However, you're trying to use an array as an index into an array, and that won't work. That is,
_array[u]
doesn't make sense. Arrays are indexed numerically. Thus the value of that expression is undefined
, and that explains the error message — you're trying to reference the "unshift" property of the value undefined
. (Well, undefined
isn't really a value, but whatever.)
use
_array[0].unshift();
instead of
_array[u].unshift('somevalue');
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.