JavaScript turn string into json object -
so, have function works json object want make simplier created function values of json object. why doesn't work?
var itemdata = { weapon: function () { return { 1: { 'name': 'dagger', 'extra_skill': 'none', 'cost': 500, 'attack': 5 }, 2: { 'name': 'pickaxe', 'extra_skill': 'mining', 'cost': 25, 'attack': 5 } } }, getweapon: function (value, x) { var obj = json.parse(value); return itemdata.weapon()[x].obj } } // outputs: dagger console.log(itemdata.weapon()[1].name) // name of weapon 1 // however, outputs: uncaught syntaxerror: unexpected token console.log('getting weapon... ' + itemdata.getweapon('name', 1))
what doing wrong?
you don't need json parsing @ working, because there have json string needs parsed.
here working example:
var itemdata = { weapon: function () { return [ { 'name': 'dagger', 'extra_skill': 'none', 'cost': 500, 'attack': 5 }, { 'name': 'pickaxe', 'extra_skill': 'mining', 'cost': 25, 'attack': 5 } ]; }, getweapon: function (value, x) { return itemdata.weapon()[x][value]; } } // outputs: dagger console.log(itemdata.weapon()[0].name) // outputs: getting weapon... pickaxe console.log('getting weapon... ' + itemdata.getweapon('name', 1))
Comments
Post a Comment