I have a json array that contains some json objects. Suppose I have a course
object like this:
{"name": "Math", "unit": "3"}
And my json array is look like this:
[{"name": "Math", "unit": "3"}, {"name": "Physics", "unit": "3"}, ...]
Now I need to get an object with it's name. For example I want to get the course with "Math" name. I know It's possible to loop through each array items and check each item name and return an object that its name is equal to "Math", But my array may be too long and this is not good to loop through long array. This is possible to access object in the array by index, for example array[0]
will be equal to {"name": "Math", "unit": "3"}
. But I want to access array with key, not index. Is there any better solution for doing that? Any help would be greatly appreciated.
You can use jQuery's .grep()
.
var newArray = $.grep(obj, function(item){ return item.name == "Math"; });
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다