So, I have this function that works to get a JSON object but I want to make it simplier so I created a function to get the values of the JSON object. Why doesn't it 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)
// Get the name of weapon 1
// however, it outputs: Uncaught SyntaxError: Unexpected token a
console.log('Getting weapon... ' + itemData.getWeapon('name', 1))
What am I doing wrong?
JSON.parseis not needed (and in fact, part of the problem).