0
courses = [{
  code: 'English',
  otherFields: '1',
  list: [{id:'1'},{name:'eng'}]
}, {
  code: 'Spanish',
  otherFields: '2',
  list: [{id:'2'},{name:'spa'}]
}, {
  code: 'German',
  otherFields: '3',
  list: [{id:'3'},{name:'ger'}]
}]

 var resultSet = $.grep(courses.list, function (e) {
 return e.code.indexOf('German') == 0;
});

console.log(JSON.stringify(resultSet));

What I want is: based on 'name' parameter I want to get everything in that particular object. Example: when I pass name=spa I should get ' id=2, name=spa'. Using the above code I get the result as undefined.

I check this question but it didn't help much. Please help.

EDIT

Sorry friends but I'm really confused. Below is the code while I can see in console (Its coming from server so I don't know how it is actually structured).

result : {Object}
 course : {Object}
  name : English
  list : {Object}
   1 : {Object}
     attr1 : value1
     attr2 : value2
   3 : {Object}
     attr1 : value1
     attr2 : value2
  other : value-other
  id : 1
 course : {Object}
  name : Spanish
  list : {Object}
   1 : {Object}
     attr1 : value1
     attr2 : value2
   3 : {Object}
     attr1 : value1
     attr2 : value2
  other : value-other
  id : 2  

When I'm using result.course.id I'm getting 1. What I want is to get the entire thing inside course based on particular name (Spanish or English). I hop I made myself clear now.

Sorry for the inconvenience that you'd to face before. Please help.

4
  • What does First all elements mean? Did you mean Find all elements? Commented Mar 22, 2014 at 8:22
  • FWIW, [{id:'2'},{name:'spa'}] is a rather unusual data structure. Commented Mar 22, 2014 at 8:23
  • Why an array of objects with different keys, instead of a single object with all those keys? Commented Mar 22, 2014 at 8:23
  • I have updated the question. Can you check it now please Commented Mar 22, 2014 at 10:02

2 Answers 2

0

You were on the right track with jQuery.grep() but did not use it correctly.

What you can do is:

$.grep(courses, function(e) { return e.list[1].name == 'spa'; })

Or more generally:

function find(name) {
    return $.grep(courses, function(e) { return e.list[1].name == name; });
}

And it will return all the elements of the courses array that match the given name.

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

Comments

0

I have written a function for you. You can do it simply by:-

function FindCourse(code) {
        for (var i = 0; i < courses.length; i++) {

            if (courses[i].list[1].name === code) {
                alert(courses[i].code);
                alert(courses[i].otherFields);
                alert(courses[i].list[0].id);
                alert(courses[i].list[1].name);
            }
        }
    }

See fiddle-http://jsfiddle.net/rMECV/4/

1 Comment

I have updated the question. Can you check it now please

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.