0

I have nested object and from that render object i want to access data according to user click. Example : in below object i loaded first question with response.pathway.question and answer with response.pathway.answers now if user click on yes then i next next key should be response.pathway.answers.Yes and if user clicks on No then next key should be response.pathway.answers.No in short what ever answer user select it should be added at last.

Now problem is i am using flatlist to show answers which return item and when i add that item at last of object key its giving error . And i know its giving error because there is no key in object like response.pathway.answers.item .So my question is how can i make this item dynamic like if user select yes then it should be like response.pathway.answers.Yes and vice versa. (Note inside answers key could be anything not only yes no).

{
"pathway": {
    "question ": "this is first question",
    "answers": {
        "Yes": {
            "question": "this question i want to access if user click on yes"
        },
        "No": {
            "question": "this question i want to access if user click on no"
        }
    }
}

}

2
  • get the response in variable and do like this response["pathway"]["answer"]["Yes"] ,you can store key name in variable and get the desired result. Commented Aug 21, 2020 at 8:00
  • suppose user selected yes then store ``var selectedOption = "yes"; var res = response.pathway.answer[selectedOption]; For more refrence see w3schools.com/js/js_objects.asp Commented Aug 21, 2020 at 8:02

2 Answers 2

2

const key = 'anything you want'
const value = 'anything you want'
.....

{
  "pathway": {
    "question ": "this is first question",
    "answers": {
         // key is dynamically
        [key]: value
    }
  }
}

you can also do like obj.pathway.annswers[key] = 'value'

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

Comments

0

I'm not sure I get the question but you might want to try this: since you want to access any key inside the pathway.answer object, what you can do is using indexing rather than dot notation to access its elements.

This means that you can access pathway.answer.Yes this way too pathway.answer['Yes'].

Now you only have to get a payload of what the user clicked on to access it (for example)

for (let buttonElem of document.querySelectorAll('.answer-button')) {
buttonElem.addEventListener('click', () => alert(buttonElem.innerHTML));
}

const data = {
  pathway: {
    question: 'whatever',
    answers: {
      yes: 'something',
      no: 'something else',
      litteralyAnythingElse: 'find something idk'
    }
  }
};

const answerDiv = document.getElementById("answer");

for (let buttonElem of document.querySelectorAll('.answer-button')) {
  buttonElem.addEventListener('click', () => {
    answerDiv.innerHTML = data.pathway.answers[buttonElem.innerHTML];
  });
}
<button class="answer-button">yes</button>
<button class="answer-button">no</button>
<button class="answer-button">litteralyAnythingElse</button>

<div id="answer">
</div>

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.