0

I am new to javascript. How do I iterate a JSON result that has convert into javascript object?

const url = 'https://api.mybitx.com/api/1/tickers?pair=XBTMYR';
 fetch(url)
 .then(res => res.json())
 //.then(json => console.log(json))
 .then(function(data) {
 let bp = data.tickers
 console.log(bp.timestamp)
 })

the object results are

[ { timestamp: 1500349843208,
 bid: '9762.00',
 ask: '9780.00',
 last_trade: '9760.00',
 rolling_24_hour_volume: '325.277285',
 pair: 'XBTMYR' } ]

I just want to print out the "timestamp" key. Thanks.

montrealist
5,71812 gold badges50 silver badges72 bronze badges
asked Jul 18, 2017 at 4:04
3

2 Answers 2

2

Put key and then the object.

console.log(bp[0].timestamp)
answered Jul 18, 2017 at 4:18
Sign up to request clarification or add additional context in comments.

Comments

0

Your result is an array, as such you can iterate it by index or by using for or .forEach.

for(var i=0; i<bp.length;i++) {
 var element= bp[i];
}

Each element in your array is an object. To access the timestamp of that element use ["timestamp"] or .timestamp

for(var i=0; i< bp.length; i++) {
 var element = bp[i];
 var timestamp = element.timestamp;
 var ts= element["timestamp"];
}

To get the first time stamp use simply use b[0].timestamp.

answered Jul 18, 2017 at 4:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.