0

We have a large array of objects:

var englishStudents = [
    {StudentId: 1, Name: "John"},
    {StudentId: 2, Name: "Jack"},
    {StudentId: 3, Name: "Jane"}
];

Need to check if another similar object is contained in this array, just by comparing one property alone.

var randomStudent = {StudentId: 1337, Name: "Foo"};

This is what I have, and it seems as though it will work, but I don't think this is the best way to do this.

var studentIds = $.map(englishStudents, function (student, index) { return student.StudentId; });
var randomStudentLearnsEnglish = false;
for (var sId in studentIds) {
    if (randomStudent.StudentId == sId) {
        randomStudentLearnsEnglish = true;
        break;
    }
}

What would be the optimized way to do this?

5
  • That seems efficient enough. Are you having performance issues? Commented Jan 31, 2013 at 22:26
  • 1
    Is the array sorted? Is there a reason they're in an array, and not in something keyed by the ID? Commented Jan 31, 2013 at 22:27
  • @elclanrs: I was looking to make the two loops in there to one, somehow. Commented Jan 31, 2013 at 22:40
  • @DaveNewton: It is an unsorted array. Commented Jan 31, 2013 at 22:41
  • @FloydPink Then unless you have some form of backing index, iteration is the only option. Commented Jan 31, 2013 at 22:47

4 Answers 4

2

You should keep student data in a hash table like JHashtable instead of the array. For mor complex scenarios, you can maintain more than one hash table, like studentsById, studentsByCountryCode, etc.

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

Comments

1

If all you want to know is if the ID exists can do this:

function checkIdExists( id){
    /* map array of matching ID, if none exists length of array is zero*/
    return  $.map(englishStudents, function (student, index) { 
               return student.StudentId==id; 
    }).get().length;
});

Use:

 if( checkIdExists( 1234)){
     /* run exists code*/
 }

1 Comment

Thanks. This is exactly what I want. Although after looking at this answer (stackoverflow.com/a/9283885/218882), I feel $.grep() would be faster, but maybe they're the same.
1

If you really want, you can create a further indexing scheme:

var englishStudents = [
    {StudentId: 1, Name: "John"},
    {StudentId: 2, Name: "Jack"},
    {StudentId: 3, Name: "Jane"}
];
 //if doing this a lot of time, take the one time hit of construction and memory
var idToNameMap = createIdToNameMap(englishStudents); //returns {'1': 'John', '2': Jack' , '3': 'Jane'}

var randomStudent = getRandomStudent();
if( idToNameMap[ randomStudent.StudentId] != undefined){ ... }

Comments

1

Just do a hash instead of an array, so:

var englishStudents = {
    1: {StudentId: 1, Name: "John"},
    2: {StudentId: 2, Name: "Jack"},
    3: {StudentId: 3, Name: "Jane"}
};

and then to retrieve, simply do:

var student = englishStudents[id];

1 Comment

your object syntax is invalid

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.