javascript - JSON Object many errors. Help out -
i have made json object , getting many errors. new json kindly help. posting here screenshots. appreciated.
[![var data\[\]= {"cars": "honda":\[ {"model":"figo" }, {"model":"city"} \], "audi": \[ {"model":"a6"}, {"model":"a8"} \] } data.cars\['honda'\]\[0\].model data.cars\['honda'\]\[1\].model data.cars\['audi'\]\[0\].model ata.cars\['audi'\]\[1\].model (var make in data.cars) { (var = 0; < data.cars\[make\].length; i++) { var model = data.cars\[make\]\[i\].model; alert(make + ', ' + model); } }][1]][1]
using jsonformatter , validator site checking code.
since mentioned
i totally novice json
i explain completely.
there little bit of syntax errors in way doing this. doing loop inside javascript object break. if intention alert models of make, here how it..
initially have this
var data = { cars:{ "honda": [ {"model":"figo" }, {"model":"city"} ], "audi": [ {"model":"a6"}, {"model":"a8"} ] } }
you have variable called data
object ({}
refers object) , object has property called cars
, property holds object({}
refers object). object has 2 properties honda
, audi
. each of properties of type array ([]
refers array). , each array further contains list of objects.
so once clear above point. let manipulate object have.
do forloop properties of cars
object , when have hold of each property lets loop array , extract model
property value.
for (var make in data.cars) { (var = 0; < data.cars[make].length; i++) { var model = data.cars[make][i].model; alert(make + ', ' + model); } }
also dont confused javascript object , json....
in above example variable data
javascript object , when json.strinfigy(data)
converting javascript object string format , string format called json...
a demo fiddle understand better.
Comments
Post a Comment