0

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?

1
  • 5
    JSON is a textual notation. If you're writing JavaScript code, you're not dealing with JSON unless you're dealing with a string. There is no JSON, at all, in your question; and therefore, JSON.parse is not needed (and in fact, part of the problem). Commented Aug 16, 2015 at 23:19

1 Answer 1

5

You actually don't need JSON parsing at all to get this working, because there is nowhere where you have a JSON string that needs to be parsed.

Here is a 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))

Sign up to request clarification or add additional context in comments.

5 Comments

I like how this answer also removes using value in getWeapon, since the intention of getWeapon seems to be to always return name anyway.
I understand what I was doing wrong now. Thank you.
It's good for classes/functions to each have a single responsibility, so I'd recommend the O.P. to clarify what specifically getWeapon is supposed to return. If the plan is to always return name, then name should be hardcoded in getWeapon. This avoids unintentionally making a typo when passing in value.
@DanF there is a tick symbol if you want to acept the answer, it gives the poster 'reputation' on the site
@MaximillianLaumeister I went to eat dinner then I forgot about it. Sorry! I always mark answers checked :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.