Created
January 9, 2018 07:20
-
-
Save ronaldlangeveld/6cde23f627dac3e501704a26317f3142 to your computer and use it in GitHub Desktop.
USING JSON WITH JAVASCRIPT BASICS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USING JSON WITH JAVASCRIPT BASICS. | |
CREDIT GOES TO: https://github.com/Fanna1119 | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400" rel="stylesheet"> | |
</head> | |
<style> | |
body, | |
html { | |
height: 100%; | |
width: 100%; | |
margin: 0; | |
padding: 0; | |
left: 0; | |
top: 0; | |
font-size: 100% | |
</style> | |
<body> | |
</body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.js"></script> | |
<script> | |
var my_data = [{ | |
"model": "coinfolio_app.user_cryptos", | |
"pk": 1, | |
"fields": { | |
"USERNAME": 1, | |
"COIN_NAME": "XRP", | |
"COIN_VALUE": "500" | |
} | |
}, { | |
"model": "coinfolio_app.user_cryptos", | |
"pk": 2, | |
"fields": { | |
"USERNAME": 1, | |
"COIN_NAME": "BTC", | |
"COIN_VALUE": "1" | |
} | |
}]; | |
console.log(my_data); | |
console.log("-----------manual printing--------------"); | |
console.log(my_data["0"]) | |
console.log(my_data["0"].fields.COIN_NAME, my_data["0"].fields.COIN_VALUE); | |
console.log(my_data["1"]) | |
console.log(my_data["1"].fields.COIN_NAME, my_data["1"].fields.COIN_VALUE); | |
console.log("-----------iterate via for loop--------------"); | |
for(var i = 0; i < my_data.length; i++) { | |
var coin_name = my_data[i].fields.COIN_NAME; | |
var coin_value = my_data[i].fields.COIN_VALUE; | |
console.log(coin_name, coin_value); | |
} | |
console.log("--------jquery iteration------------"); | |
$.each(my_data, function(idx, obj) { | |
console.log(obj.fields.COIN_NAME, obj.fields.COIN_VALUE); | |
}); | |
//idx = index | |
// obj = json object | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment