jquery - Find all elements of specific object from object array -


i had posted question here did not answer worked me (may wasn't clear enough question). i'm posting here again want.

below code while can see in console (its coming server don't know how 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   

based on suppose structure be:

results = {   other: 'something',   id: '1',   courses: {list:{...},name:'english'}   }, {   other: 'again-something',   id: '2',   courses: {list:{...},name:'spanish'}   }, {   other: 'again-something',   id: '3',   courses: {list:{...},name:'german'} }; 

what want is: elements of result.course result.course.name = 'spanish'

i tried below code return empty array:

var test = $.grep(result, function(e) { return e.courses.name == 'spanish'; }); console.log(json.stringify(test)); 

i guess problem result object , not array. if result = [{...}]; somehow code works.

can please tell me how work out when result object.

thanks.

i not sure exact object hierarchy have in scenario, but:

i created array of objects below:

var o = [{   result: {     course: {         name: "english",        list: [{ attr1: "v1", attr2: "v2" }, { attr1: "v5", attr2: "v6" }],        other: "value-other",        id: "1"     }   } }, {   result: {     course: {         name: "spanish",        list: [{ attr1: "v1", attr2: "v2" }, { attr1: "v5", attr2: "v6" }],        other: "value-other",        id: "1"     }   } }, {   result: {     course: {         name: "french",        list: [{ attr1: "v1", attr2: "v2" }, { attr1: "v5", attr2: "v6" }],        other: "value-other",        id: "1"     }   } }] 

executing var test = $.grep(o, function(e) { return e.result.course.name == "spanish"; } ); returns object course name spanish (2nd element array).

grep expects array - o array of objects structure of result -> course -> name, list, other, id.

grep loops on each element of array - each element of array object single property - "result" - contents, in turn, "course" object, properties name, list, other , id.

that's why within grep access e.result.course.name.

hope sheds light.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -