There are JSON: {"Gray":"11","Black":"18"} .
How to cycle through it so that you can use a key and a value?
(key => value, as in the foreach loop in php)
There are JSON: {"Gray":"11","Black":"18"} .
How to cycle through it so that you can use a key and a value?
(key => value, as in the foreach loop in php)
This example: {"Gray":"11","Black":"18"}
is an object, to bypass the properties of an object, you can use the for..in . This will check all enumerated properties, including the properties of ancestors.
var o = { "Gray": "11", "Black": "18" }; for (var key in o) { console.log(key, ':', o[key]); } You can also use the Object.keys function .
var o = { "Gray": "11", "Black": "18" }; Object.keys(o).forEach(function(key) { console.log(key, ':', this[key]); }, o); Better to use the jquery $ .each function
$.each(JSON.parse('{"Gray":"11","Black":"18"}'), function(key, value) { console.log(key + " " + value); }) var json = '{"Gray":"11","Black":"18"}'; // Конвертируем JSON в объект var obj = JSON.parse(json); // Работаем с объектом for (var prop in obj) { console.log(prop, obj[prop]); } use the for..in construct
var data = {"Gray":"11","Black":"18"}; for (color in data) alert("Color: " + color + ", Value: " + data[color]; Source: https://ru.stackoverflow.com/questions/574461/
All Articles