javascript - Sorting/Filtering JSON based on certain property -
so got response api. want build select box types, how extract json related skill_level json without using loops.
[ { "id": 32, "name": "beginner", "type": "skill_level" }, { "id": 33, "name": "intermediate", "type": "skill_level" }, { "id": 34, "name": "experienced", "type": "skill_level" }, { "id": 35, "name": "professional", "type": "skill_level" }, { "id": 36, "name": "expert", "type": "skill_level" }, { "id": 37, "name": "male", "type": "sex" }, { "id": 38, "name": "female", "type": "sex" }, { "id": 39, "name": "single", "type": "marital_status" }, { "id": 40, "name": "married", "type": "marital_status" }, { "id": 41, "name": "divorced", "type": "marital_status" }, { "id": 42, "name": "not wish say", "type": "marital_status" } ]
check out array.prototype.filter
:
var skilllevels = data.filter(function(item) { return item.type === 'skill_level'; });
from docs, works follows:
the filter() method creates new array elements pass test implemented provided function.
assuming data
refers array provided in question, result skilllevels
being new array containing of items item.type
equal "skill_level"
.
Comments
Post a Comment