Struggling with fetching JSON objects and need help accessing the data within the "choices" key. Grateful for any guidance you can provide.
Struggling with fetching JSON objects and need help accessing the data within the "choices" key. Grateful for any guidance you can provide.
Example of a loop
for (let i = 0; i < data.questions.length; i++) {
console.log(data.questions[i].choices.a);
console.log(data.questions[i].choices.b);
}
Iterating through the questions array to display choices for each question:
for (let i=0; i<questions.length; i++) {
let options = questions[i].choices;
console.log(options.A);
console.log(options.B);
}
If you've defined a variable assignment like var myobj = { ... }
where the ...
represents your JSON object in the post, then to access all the data points related to choices, you can use the following code snippet:
myobj.questions[0].choices.a
myobj.questions[0].choices.b
myobj.questions[1].choices.a
myobj.questions[1].choices.b
Are you looking for a more specific explanation on how to iterate through all questions and choices?
Note: Based on your comment, it seems that you might be interested in something similar to this code block:
for (var qi=0; qi < myobj.questions.length; qi++) {
var q = myobj.questions[qi];
console.log(q.ques);
for (var choiceKey in q.choices) {
console.log(" " + choiceKey + " --> " + q.choices[choiceKey]);
}
}
You can modify the logic within the loops as per your requirements. Running the above code with your example JSON will produce output like below:
how old are you?
a --> 19
b --> 20
question two?
a --> answer1
b --> answer2
Just getting started with coding, I recently created a collapsible accordion in a NextJs app using the react-collapse package. Everything seems to be working fine, but I'm running into an issue with keeping track of which 'drawer' is current ...
I have been working on implementing an object-oriented approach in my program. According to what I've learned, there should be an inheritance relationship between World and Sprite classes, with Sprite as the parent. However, when I try to call Sprite. ...
In my current project, I've been working on a middleware that is responsible for extracting the user model and attaching it to the request pipeline. Although I have successfully implemented a token extractor middleware that attaches the token to the r ...
After successfully integrating Swagger API documentation with my rest services, I encountered a challenge. The Swagger page appears too lengthy due to the numerous response classes in my project, requiring users to scroll extensively to find information. ...
function show(){ alert("i am pixel"); } function disableImgClick(){ $(".Dicon").unbind('click'); } $(document).ready(function(){ $("#turnoff_btn").click(function (e){ e.preventDefault(); disableImgClick(); }); }); i have a group of ima ...
Spent the entire day yesterday attempting to make my page responsive while Zooming In, but I just can't seem to get it right. Even after adding height and weight, elements still get mixed up when zooming in on the page. I've scoured countless w ...
Currently, I am attempting to set it up so that when a user clicks on a profile, they are redirected to the profile page of the user they clicked on. Here is the code I am using: const self = this; browserHistory.push({ pathname: '/user ...
I have implemented a mix of bootstrap 3 and manual coding for my project. While the image thumbnails function correctly when clicked, the span arrows do not navigate to the next or previous images as intended. Below is the code snippet: var maximages ...
I need to apply the sticky class to the navbar when a user scrolls down, and remove it when they scroll back up. To achieve this, I am attempting to calculate the scrolled height. Currently, my code looks like: mounted(){ this.setUpRoutes(); wind ...
I am currently developing an application that requires fetching data asynchronously and preserving the state in the parent component while also passing the data reference to children components. I encountered an issue where the props do not update when the ...
I am working with a camera that needs to rotate around a specific target position within the scene. Despite attempts to implement a pivot concept, as suggested in this discussion: https://github.com/mrdoob/three.js/issues/1830, I have come up with my own s ...
In my MVC project, there's a standard form with a dropdown list. Each time I change the dropdown value, the entire page reloads due to a postback. This behavior is unexpected, and I want to prevent the dropdown from triggering a postback. Strangely, ...
OBJECTIVE: The objective is to show the ToolkitArea only when there are children present in "#toolkitArea". CHALLENGE: Unable to accurately determine the number of children inside the ToolkitArea. ACTIONS TAKEN SO FAR: I have developed a component calle ...
https://i.sstatic.net/xzJWT.png Hello! I am currently working on implementing Dropzone.js to create a gallery. I've encountered a small issue where the default behavior of Dropzone displays a blank container area with a clickable effect to upload pho ...
Having trouble rendering blog posts from a json file in React const BlogPost = (props) => { const [post, setPost] = useState({ id: "", blogCategory:"", blogTitle:"", postedOn:"", ...
I could really use some assistance with this issue. I have a bunch of text blocks containing links and have been utilizing linkifyjs's React component to automatically wrap the links in anchor tags. However, now I am looking to add a custom button nex ...
Is there a way to play video/image files in full screen mode on a browser? How can I achieve this? ...
My table is not loading correctly via ajax. Here is the JavaScript code I am using: $('#initAjaxDataTable').DataTable( { "pagingType": "full_numbers", "processing": true, "serverSide": true, "ajax": { "url": $('#ini ...
I've been working on a feature where users are redirected to different URLs based on the page they click the login button on. For example, I have a registration page and a regular page, both with a login button. When a user clicks the login button on ...
Hello everyone, thank you for stopping by. I'm trying to figure out the correct way to achieve the following: <script language="javascript"> function flag(nation) { this.nation=nation; document.getElementById("flag").innerHTML="<img src=&ap ...