Apps
Apps

Reputation: 3389

JavaScript object data and Arrays

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?

Upvotes: 1

Views: 215

Answers (5)

jbabey
jbabey

Reputation: 46647

[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.

Upvotes: 1

Wayne
Wayne

Reputation: 60414

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

Upvotes: 1

Sirko
Sirko

Reputation: 74076

[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.

Upvotes: 1

Maz
Maz

Reputation: 3375

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

Upvotes: 2

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28359

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

Upvotes: 0

Related Questions