0

Trying to organize a questions based game layout using JSON. Is it better to add an ID identifier into each question or can I use an inbuilt id identifier through each question? The functionality is when a user first clicks on the first question it loads the subQuestion into the next slide. Thanks

There are two ways I think it can be done.

Version 1:

"question": {
    "id": "0",
    "name": "Incentive Trip",
    "subQuestion": {
        "name": "Bond Up With Clients",
        "subsubQuestion": {
            "1,421": "Experiencing Adventures",
            "2,284": "Immersing Into Culture",
            "3,394": "Overcoming Challenges"
        },
        "name": "Increase Loyalty",
        "subsubQuestion": {
            "1,212": "Adding Value",
            "2,930": "Making Difference",
            "3,489": "Starting VIP club"
        },
        "name": "Reward Team",
        "subsubQuestion ": {
            "1,329": "Being A Tourist",
            "2,586": "Enjoying Sunshine",
            "3,498": "Surprising Colleagues"
        }
    },
},

Version 2 - No identifier, just nested:

"Marketing Event" : {
    "Encourage Relationship": {
        "1,398": "Knitting Web",
        "2,59": "Rewarding Favourites",
        "3,378": "Top Hatting"
    },
    "Increase Awareness": {
        "1,102": "Enjoying The Vibe",
        "2,495": "Interacting Through Brand",
        "3,697": "Standing Out"
    },
    "Launch Product": {
        "1,287": "Educating Masters",
        "2,495": "Shocking Audience",
        "3,394": "Steering Wheel"
    }
},
Sayed Mohd Ali
  • 2,004
  • 3
  • 8
  • 25
Jesse C
  • 347
  • 5
  • 19

1 Answers1

2

solution 1

Check this answer. Object properties are not in a guaranteed order. So you should definitely add an id if you want to make sure that your users really get the next question.

solution 2

What about mixing mixing up Arrays and objects? Your ids are then the array indexes and the order is guaranteed, as mentioned here.

[
    "Marketing Event" : {
        "Encourage Relationship": {
            "1,398": "Knitting Web",
            "2,59": "Rewarding Favourites",
            "3,378": "Top Hatting"
        },
        "Increase Awareness": {
            "1,102": "Enjoying The Vibe",
            "2,495": "Interacting Through Brand",
            "3,697": "Standing Out"
        },
        "Launch Product": {
            "1,287": "Educating Masters",
            "2,495": "Shocking Audience",
            "3,394": "Steering Wheel"
        }
    },
    "Another Event" : {
        "Another Relationship": {
            "1,398": "Knitting Web",
            "2,59": "Rewarding Favourites",
            "3,378": "Top Hatting"
        },
        "Another Awareness": {
            "1,102": "Enjoying The Vibe",
            "2,495": "Interacting Through Brand",
            "3,697": "Standing Out"
        },
        "Another Product": {
            "1,287": "Educating Masters",
            "2,495": "Shocking Audience",
            "3,394": "Steering Wheel"
        }
    }
]
Community
  • 1
  • 1
Marc Dix
  • 1,565
  • 10
  • 26
  • Problem when using the second solution is that when I loop through the data the object number isn't returned using: $(data).each(function(i,val){ console.log(i); }); – Jesse C May 31 '16 at 08:02
  • But isn't the counter your object id then? The first element is 0, second element is 1 and so on? Mhm, not sure if we have some misunderstanding: Can you please explain further? – Marc Dix May 31 '16 at 08:12
  • I'm building the JSON array myself, so it can be whichever is easiest to return the intended result. Ideally i'd like to assign the li the id value then I can use filter to load in the next set of questions. – Jesse C May 31 '16 at 08:14
  • Sorted it wth your help. If I assign an ID value underneath each question I can then use the element.id to populate the li value. Thanks! – Jesse C May 31 '16 at 08:22