3

My JSON is as follows:

{
    "sales": [{
        "manager": "alberic",
        "surgeon": "Dr Barry Biggins",
        "amount": "300",
        "country": "USA",
        "percent-seller": "30",
        "antiquity": "June 2017",
        "date": "6"
    }, {
        "manager": "support",
        "surgeon": "Dr Barry Biggins",
        "amount": "300",
        "country": "UK",
        "percent-seller": "20",
        "antiquity": "June 2017",
        "date": "2"
    }, {
        ...
    }]
}

I want to retrieve the objects from sales where manager = "support" and date = "2". How do I go about this in jQuery?

Thanks!

3 Answers 3

2

Simply you can use filter() method and use your condition inside filter method function will return element if your condition become true otherwise it ignore element.

data=    {"sales": [{
          "manager": "alberic",
          "surgeon": "Dr Barry Biggins",
          "amount": "300",
          "country": "USA",
          "percent-seller": "30",
          "antiquity": "June 2017",
          "date": "6"
        }, {
          "manager": "support",
          "surgeon": "Dr Barry Biggins",
          "amount": "300",
          "country": "UK",
          "percent-seller": "20",
          "antiquity": "June 2017",
          "date": "2"
        },
       ]
      };


var matchedElements = data.sales.filter(function(element) {
   return (element.manager == 'support' && element.date == '2');
});
                   
console.log(matchedElements);
                    
//if you want to access  surgeon of first element of matchedElements
                    
console.log(matchedElements[0].surgeon);

          
//if you want to access  surgeon of all elements in matchedElements
for(i in matchedElements)           
{
console.log(matchedElements[i].surgeon);
}

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but how do I access surgeon from matchedElements after using it?
I have modified my code for access surgeon from matchedElements :)
1

You filter the sales array.

Make sure to add the polyfill from the above link if you want to support older browsers.

var matchingSales = jsonData.sales.filter(function(sale) {
   return sale.manager == 'support' && sale.date == '2';
});

Comments

1

var data = {
  "sales": [{
    "manager": "alberic",
    "surgeon": "Dr Barry Biggins",
    "amount": "300",
    "country": "USA",
    "percent-seller": "30",
    "antiquity": "June 2017",
    "date": "6"
  }, {
    "manager": "support",
    "surgeon": "Dr Barry Biggins",
    "amount": "300",
    "country": "UK",
    "percent-seller": "20",
    "antiquity": "June 2017",
    "date": "2"
  }]
};


$.each(data.sales, function(i, v) {

  if (v.manager == 'support' && v.date == '2') {
    console.log(v.manager)
    console.log(v.surgeon)
    console.log(v.amount)
  }

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Iterate over them using .each()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.