0

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!

3 Answers 3

3

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 :) )

Sign up to request clarification or add additional context in comments.

1 Comment

Well now that I have it working correctly I'm actually just going to push the values that were going into myArray straight into hashArray. Thanks again!
1

you wraped the object containing hashArray in an array. To access the field hashArray you have to do the following:

item[0].hashArray.push(...)

Comments

1
item[0].hashArray.push(myArray[i]);

or

var item = {    hashArray: []     };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.