1

I have a JavaScript object as follows.

var sampleData = {
    "studentsList": {
        "Student1": {
            "marks": "123",
            "grade": "B"
        },
        "Student2": {
            "marks": "144",
            "grade": "A"
        }
    }
};​

Now the user enters the name of a student, say Student1 and I need to get the details of that object.

I have two questions.

[1] How do I get the details of the entered student using JavaScript?

When I use sampleData.studendsList.valueOf("Student1") returns me the complete object. I just need the details of "Student1".

[2] Is this the correct way to do it? Or we should create an array of Students and that contains an property called "name" with value say Student1 ? If I go with this approach then I need to iterate through the entire array list to get the details of a student.

Which appraoch is better?

4
  • 1
    There are no Arrays present in the above code. A better way would be to structure it to use Arrays as they are the more natural structure here. Commented Jul 10, 2012 at 16:39
  • You don't really need to have a sampleData variable wrapping the array if you don't have any other sample data. You could just have var studentsList = {blah}. Commented Jul 10, 2012 at 16:39
  • @pst, In the above example, I don't have an array. But I can have an array of Students and name can be one attribute in that array. I think it is the best way to represent it, but performance wise, I'm not sure if that is the right approach to go by. Commented Jul 10, 2012 at 16:43
  • @Apps O(n) is only generally applicable when n increases to "a non-small value". Unless there is a performance concern, represent the data as is best fit for the current problem. It is very easy to create a cross-lookup from an Array or to Sort object properties based on their corresponding values. Commented Jul 10, 2012 at 17:07

5 Answers 5

2

sampleData.studentsList.Student1 OR sampleData.studentsList["Student1"]

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

Comments

1

An array is usually the more natural way to store a list of items (like students). But there are tradeoffs. As structured (using objects), you can access any student by name in constant time:

sampleData.studentsList.Student1

Structuring with an array looks like this:

var sampleData = {
    "studentsList": [
        {
            "marks": "123",
            "grade": "B"
        },
        {
            "marks": "144",
            "grade": "A"
        }
    ]
};​

Access the nth student like this:

sampleData.studentsList[n]

If the index of the desired student is not known, then accessing a particular student (based on its details) is O(n) (because you'd need to iterate the entire array to find the correct item in the worst case).

Comments

1

[1] Access the property by using the access via []. That way you can insert a variable there instead of just a hardcoded identifier.

var studentDetails = sampleData.studendsList[ "Student1" ];

[2] For fast access to a specific student, this the better approach imo. Any array would involve some kind of array scanning to access a specific details object.

Comments

1

[1] given a student name stored in a javascript variable, let's call it studentName, you would access that student's details like so:

var studentData = sampleData.studentsList[studentName];

[2] the big determining factor when choosing between an array or an object is: what does the key mean? if the key is irrelevant or just numeric, it should be an array. in this case, the key is the name of a student, so having an object is definitely the better choice.

Comments

0

EDIT... I just realized that you're just building the object in place (rather than having it as a String)... so in fact you can skip the parsing and just get to the object directly via..

sampleData.studentsList.Student1

If however you had that object in the exact shape you have it but as a String you'd need to JSON.parse it first like so...

 myObj = JSON.parse(yourString);

    myObj.studentsList.Student1

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.