0

Noob question. Setting array elements throws an error.

I get this error when I run the script: array1[user_id] is undefined.

array1 = new Array();

// the data variable I got from a JSON source
// using jQuery
$.each(data, function(i, item) {

    // Set variables
    user_id = item.post.user_id;
    user = item.post.user;
    price = item.post.price;

    if (array1[user_id]['user'] != user) {
        array1[user_id]['price'] = price;
        array1[user_id]['user'] = user;
    }

}
4
  • Well.. I am trying to make more sense of it now... Commented Jun 11, 2011 at 0:33
  • It's not that bad, Cudos is clearly trying to define nested objects and doesn't realize each sub-object needs to be initialized independently. Commented Jun 11, 2011 at 0:44
  • @pst I am trying my best. I don't know about you but English is not my first language and javascript is pretty new to me. No reason to be rude. Commented Jun 11, 2011 at 0:48
  • @pst: did the OP not explain that the problem was that array1[user_id] is undefined? Commented Jun 11, 2011 at 1:00

2 Answers 2

2

First, you should not use an array, if you need a hash map, use objects. In some languages, it's one thing, but in JS they're not.

When defining a nested object you have to define each level as an object.

var hash = {};

// the data variable I got from a JSON source
$.each(data, function(i, item) {

    // Set variables
    user_id = item.post.user_id;
    user = item.post.user;
    price = item.post.price;
    // Have to define the nested object
    if ( !hash[user_id] ) {
        hash[user_id] = {};
    }
    if (hash[user_id]['user'] != user) {
        hash[user_id]['price'] = price;
        hash[user_id]['user'] = user;
    }    
}
Sign up to request clarification or add additional context in comments.

6 Comments

Who said anything about jQuery?
@Ivan - Nobody, but it often times makes life eas(ier).
@Ivan: the poster did, I just modified his original posting so it would work, I didn't add any jQuery. Read before you post and downvote!
Ah, you're right. The poster should have mentioned it somewhere since I was thinking of a non-jquery solution.
@Ivan: What was the poster supposed to mention? I also can't stand people that use jQuery for every answer, but the question is clearly not about jQuery, it just uses jQuery as part of the example.
|
0

If I understand this question correctly, you have to initialize the first dimension of array1 first, like so:

array1[user_id] = {};

then you can do this:

array1[user_id]['user'] = 'whatever';

That is of course assuming your user_id is not undefined.

2 Comments

Problem is that I don't know what the "user_id" variable is before I loop through the "data" variable.
Then I would use an object and try catch.

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.