A
A
Alexander2015-10-12 15:55:05
JavaScript
Alexander, 2015-10-12 15:55:05

JSON data filter?

I have a JSON array:

var json = [{
  "variation_id": 618,
  "attributes": {
    "attribute_pa_color": "navy",
    "attribute_pa_size": "l"
  },
  "sku": "618"
}, {
  "variation_id": 619,
  "attributes": {
    "attribute_pa_color": "gray",
    "attribute_pa_size": "l"
  },
  "sku": "619"
},
{
  "variation_id": 620,
  "attributes": {
    "attribute_pa_color": "navy",
    "attribute_pa_size": "s"
  },
  "sku": "620"
}, {
  "variation_id": 621,
  "attributes": {
    "attribute_pa_color": "gray",
    "attribute_pa_size": "s"
  },
  "sku": "621"
}];

you need to filter the data by color navy ("attribute_pa_color": "navy")
to display an array like this:
{
  "variation_id": 618,
  "attributes": {
    "attribute_pa_color": "navy",
    "attribute_pa_size": "l"
  },
  "sku": "618"
},{
  "variation_id": 620,
  "attributes": {
    "attribute_pa_color": "navy",
    "attribute_pa_size": "s"
  },
  "sku": "620"
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Inchin ☢, 2015-10-12
@alexsandr_s

var newJson = json.filter(function(e){
   return e.attributes.attribute_pa_color == "navy";
});

A
Alexey Ukolov, 2015-10-12
@alexey-m-ukolov

Sequentially pass through the array, get the value of the desired property of the object, compare.
Which of these are difficult for you?

S
Sergey Lysogor, 2015-10-12
@serhioli

It has already been told here:
stackoverflow.com/questions/18504285/how-to-filter...
And in general, there are a lot of things on Google for this query: json filter by attributes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question