Q:

Sorting/Filtering JSON based on certain property

So I got this response from an API.
I want to build a select box from all types, how do I extract only JSON related to skill_level from this 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 To Say",
    "type": "marital_status"
  }
]

A:

Check out Array.prototype.filter:
var skillLevels = data.filter(function(item) {
    return item.type === 'skill_level';
});

From the docs, this works as follows:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Assuming that data refers to the array you provided in your question, this will result with skillLevels being a new array containing all of the items where item.type was equal to "skill_level".

