Skip to content

Latest commit

 

History

History
46 lines (40 loc) · 854 Bytes

filter-a-json-array.md

File metadata and controls

46 lines (40 loc) · 854 Bytes

Filter A JSON Array

Category: JavaScript

You can filter a JSON array by matching on elements.

Given the following JSON array called cars:

[
    {
        "manufacturer": "BMW",
        "car": "M3 Competition"
    },
    {
        "manufacturer": "BMW",
        "car": "X5 M Competition"
    },
    {
        "manufacturer": "Honda",
        "car": "Civic Type R"
    },
    {
        "manufacturer": "Ford",
        "car": "Mustang GT"
    },
    {
        "manufacturer": "Porsche",
        "car": "911 GT3"
    },
    {
        "manufacturer": "Volkswagen",
        "car": "Golf GTI Clubsport"
    },
    {
        "manufacturer": "Volkswagen",
        "car": "T-ROC R"
    }
]

To return items where the element matches a specific value:

var filteredItems = cars.filter((car) => car.manufacturer === "BMW");