-4
[{
    "Id": 2,
    "Stock": [{
        "Id": 1,
        "available": 4,
        "Price": 188.03
    }, {
        "Id": 24703,
        "available": 6,
        "Price": 175.35
    }, {
        "Id": 49405,
        "available": 10,
        "Price": 249.52
    }, {
        "Id": 74107,
        "available": 13,
        "Price": 226.46
}]

Based on the above JSON object using JavaScript, how would I be able to get the value for price field based on the available field? I understand how to get the price value of, say, stock at position 1, however what I would like to do is get the price value based on the condition value.

AlastairG
  • 4,119
  • 5
  • 26
  • 41
SEED
  • 49
  • 10
  • do you already use JQuery? Do you want a solution with or without JQuery? The short answer is: fetch it with a ajax get request – Michael Ritter Jan 23 '17 at 16:12
  • i am using jQuery to get any price of stock[i], what i need is to get the price based on the available value – SEED Jan 23 '17 at 16:13
  • Another question: are you asking how to fetch from file or just how to get a specific object out of the stock array based on some criteria? – Michael Ritter Jan 23 '17 at 16:14
  • You can use `Array.filter` to get the object based on your condition in the available value. – Anthony C Jan 23 '17 at 16:14
  • Take a look at http://stackoverflow.com/questions/12462318/find-a-value-in-an-array-of-objects-in-javascript – Snowmonkey Jan 23 '17 at 16:15
  • i am fetching from file how ever i do know how to do that,once i do that i want to get stock price based on the criteria of available – SEED Jan 23 '17 at 16:15
  • @Yusuf'Seed'Seedat can you give us a example input and example output? Like: whats the input? minimum available? and what should it output? all objects that match that? or only one price? add that to your question – Michael Ritter Jan 23 '17 at 16:20
  • Possible duplicate of [Find a value in an array of objects in Javascript](http://stackoverflow.com/questions/12462318/find-a-value-in-an-array-of-objects-in-javascript) – Anthony C Jan 23 '17 at 16:52

3 Answers3

1

You could try the find() method for an array to find the specific element.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
Leon
  • 149
  • 8
0

try

var list_of_wanted_stock = json_value.Stock.filter( function(e) {
  return (/*condition for the stock you want where e is a stock object*/);
})
Dominique Fortin
  • 2,212
  • 15
  • 20
0

you can use map and filter as follows, the below function finds stocks with available =10, you can modify this condition as per your requirements

        var data = [{
        "Id": 2,
        "Stock": [{
            "Id": 1,
            "available": 4,
            "Price": 188.03
        }, {
            "Id": 24703,
            "available": 6,
            "Price": 175.35
        }, {
            "Id": 49405,
            "available": 10,
            "Price": 249.52
        }, {
            "Id": 74107,
            "available": 13,
            "Price": 226.46
        }]}];

        var dataToBeSearched = data[0].Stock;

        var result = dataToBeSearched.map(function(a){
                      if(a.available == 10){
                      return a;
                      }
                      }).filter(function( element ) {
                   return element !== undefined;
                   });
        console.log(result);
Mitesh Pant
  • 524
  • 2
  • 15