Reputation: 51443
I have an array full of values, myArray[]
I'm trying place this array in a hash table to pass over the socket from my node.js server.
I want the array in the hash table to contain all the same information as myArray.
var item = [
{ hashArray: [] }
];
for (var i = 0; i < myArray.length; i++) {
item.hashArray.push(myArray[i]);
}
I receive the error that I can't call push of undefined.
Thanks for any help!
EDIT: Thanks very much everyone, I see what I was doing wrong!
Upvotes: 0
Views: 1311
Reputation: 31498
You're creating item
as an array with an object on the zero'th index:
var item = [
{ hashArray: [] }
];
Either let item
be the object:
var item = {
hashArray: []
};
I assume this to be what you want, unless item
is meant to be an array, in which case you should push()
to item[0]
:
item[0].hashArray.push(myArray[i]);
EDIT
On a side-note, why not just let the hashArray
array hold the values from myArray
?
var item = {
hashArray: myArray
};
(Asking out of curiosity here :) )
Upvotes: 3
Reputation: 439
item[0].hashArray.push(myArray[i]);
or
var item = { hashArray: [] };
Upvotes: 1
Reputation: 6787
you wraped the object containing hashArray
in an array. To access the field hashArray
you have to do the following:
item[0].hashArray.push(...)
Upvotes: 1