0

I want to use a String as a part of the addressing to json data using jQuery.

 var group = "AA";
 var products = {
        "AA": [
                   { "name": "1", "image": "/" },
                   { "name": "2", "image": "/" },
                   { "name": "3", "image": "/" }
        ]}
var name2 = products.parseSelector(group)[1].name;

The idea is to get 2 as result. But it throws an Exception.

Any help would be appreciated.

4
  • What is parseSelector()? I've never seen that before. Commented Aug 26, 2016 at 16:40
  • 2
    var name2 = products[group][1].name; Commented Aug 26, 2016 at 16:41
  • PROTIP: When asking a question, include the text of the error/exception. Don't just say "it throws an Exception". Commented Aug 26, 2016 at 16:44
  • P.S. This question has nothing to do with JSON. JSON is a string representation of data that just so happens to resemble the code used to create a JavaScript object. You have an object in JavaScript code, you do not have JSON. Commented Aug 26, 2016 at 16:47

1 Answer 1

2

Use bracket notation to access the object property.

var name2 = products[group][1].name;
Sign up to request clarification or add additional context in comments.

Comments

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.