1

I tried to get the BidPrice object. I am getting this data in an EventSource and would like to process it.

    var data = {
      "status" : "OK",
      "Quote" : {
        "EUR/USD" : {
          "eventSymbol" : "EUR/USD",
          "bidTime" : 1444932337000,
          "bidExchangeCode" : "",
          "bidPrice" : 1.29805,
          "bidSize" : 1,
          "askTime" : 1444932337000,
          "askExchangeCode" : "",
          "askPrice" : 1.29807,
          "askSize" : 1
        },
        "USD/AUD" : {
          "eventSymbol" : "USD/AUD",
          "bidTime" : 1444932337000,
          "bidExchangeCode" : "",
          "bidPrice" : 0.97457,
          "bidSize" : 1,
          "askTime" : 1444932337000,
          "askExchangeCode" : "",
          "askPrice" : 0.97464,
          "askSize" : 1
        }
      }
    };

    for(var key in data){
        console.log(data[key].Quote.bidPrice);
        //my goal is to output this: console.log('EUR/USD bid price is 1.29805');
    }
1
  • data.Quote["EUR/USD"].bidPrice <- should be straight forward. Commented Oct 15, 2015 at 18:19

2 Answers 2

1

What you have is close. You need to iterate through the keys on data.Quote instead. See the working example below, which uses the forEach method to loop through the quote object's keys.

The basic idea is to do this:

Object.keys(data["Quote"]).forEach(function (k) {
    console.log(k + " bid price is: " + data["Quote"][k]["bidPrice"]);
});

And the full example:

var data = {
  "status" : "OK",
  "Quote" : {
    "EUR/USD" : {
      "eventSymbol" : "EUR/USD",
      "bidTime" : 1444932337000,
      "bidExchangeCode" : "",
      "bidPrice" : 1.29805,
      "bidSize" : 1,
      "askTime" : 1444932337000,
      "askExchangeCode" : "",
      "askPrice" : 1.29807,
      "askSize" : 1
    },
    "USD/AUD" : {
      "eventSymbol" : "USD/AUD",
      "bidTime" : 1444932337000,
      "bidExchangeCode" : "",
      "bidPrice" : 0.97457,
      "bidSize" : 1,
      "askTime" : 1444932337000,
      "askExchangeCode" : "",
      "askPrice" : 0.97464,
      "askSize" : 1
    }
  }
};

//Log function for demonstration
function log (s) {
  var e = document.createElement('pre');
  e.innerHTML = s;
  document.body.appendChild(e);
  console.log(s);
}

Object.keys(data["Quote"]).forEach(function (k) {
  log(k + " bid price is: " + data["Quote"][k]["bidPrice"]);
});

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

Comments

0
console.log(data.Quote['EUR/USD'].bidPrice);

The key here will be status and Quote.

for(var key in data){
    console.log(key);
}

what you really want is (as far as I understand). Key are EUR/USD and USD/AUD

for(var key in data.Quote){
   console.log(data.Quote[key].bidPrice);
}

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.