"Exploring the Possibilities of AngularJS with JSON

Here is some JSON data that I am working with:

  {
            "_id" : ObjectId("542e65368a1cec1227ae2bac"),
            "result" : {
                    "full" : {
                            "Array1" : [
                                    "mytext1",
                                    "mytext2"
                            ],
                            "Array2" : [
                                    "mytext3",
                                    "mytext4"
                            ]
                    }
            }
    }

To retrieve everything: OK

console.log("response ", response); 

To get the _id value: OK

console.log("_id ", response._id);

However, I am having trouble accessing mytext1, mytext2, and so on...

How can I proceed using angularjs?

Thank you for your assistance!

Answer №1

response, result, and full are all objects, while Array1 and Array2 are arrays.

To access the first item in Array1:

response.result.full.Array1[0]

To access the second item in Array1:

response.result.full.Array1[1]

To access the first item in Array2:

response.result.full.Array2[0]

To access the second item in Array2:

response.result.full.Array2[1]

If you need to log every item in an array, you can use a simple for...loop:

var arr = response.result.full.Array1;
for (var i = 0, l = arr.length; i < l; i++) {
  console.log(arr[i]);
}

Answer №2

let data = {
        "_id" : "abcdefg123456789",
        "output": {
                "complete" : {
                        "List1" : [
                                "word1",
                                "word2"
                        ],
                        "List2" : [
                                "word3",
                                "word4"
                        ]
                }
        }
};

//Loop through the arrays List1, List2 (and any additional ones)
for(let list in data.output.complete)
{
    //Loop through the elements of each array
    for(let j = 0; j < data.output.complete[list].length; j++)
    {
        console.log(data.output.complete[list][j]);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Destructuring and For of Loop in ES6: Capturing the first object only

Working with my react app, I have a specific object called 'topics' that I am looping through: const topics = [ {name: 'Mon', color: 'blue', id: 1}, {name: 'Tue', color: 'red', id: 2}, {name: 'W ...

Verify whether the values in two columns across distinct tables are matching and provide a result

I am working with 2 separate tables. table1 consists of the fields: year,day,month,name and table2 has these fields: years,gift The objective is to retrieve the rows where year matches years. It is crucial that the two queries are executed independentl ...

Is there a way to transfer HTML code from a textarea to CKEDITOR for setData?

My goal is to retrieve data from a textarea element and use it as setData for CKEDITOR. However, instead of receiving the HTML code, I am only getting the code as a string, which is not rendering properly as HTML. Here's the code snippet: CKEDITOR.re ...

Is there a way to remove the initial number entered on a calculator's display in order to prevent the second number from being added onto the first one?

I am currently in the process of developing a calculator using HTML, CSS, and JavaScript. However, I have encountered an issue with my code. After a user inputs a number and then clicks on an operator, the operator remains highlighted until the user inputs ...

Can someone provide an explanation of the Typescript peerDependencies in Angular, specifically comparing versions tslib 1.* and 2.3.*?

I am in the process of starting a new angular project, but I'm facing difficulties in importing the localStorage feature. I referred to an existing project that utilized localStorage in the following way: import { Injectable } from '@angular/core ...

What steps should I follow to display three.js in a web browser?

Every time I attempt to view my progress with three.js on my local browser, it fails to load and gives me this error message, Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/" ...

The consistent failure of the 201 status node express API is causing major

I am currently working on creating an API using Express. However, when I receive a response from the server, it shows '201 created'. The issue arises when I attempt to make an HTTP request through promises and encounter a false interpretation of ...

Altering Collada texture information during the loading process of the .jpg file

Is there a way to modify the texture image data, such as changing the .jpg header text, when loading the .jpg texture in three.js? I am curious if the texture data is accessible somewhere within the code, possibly as a string. How could I go about this? ...

How can I arrange a JQM list view alphabetically?

I have data in JSON format with key-value pairs: { "535826": "Adamov", "536156": "Bečice", "544272": "Borek", "544281": "Borovany", "535681": "Borovnice", "544299": "Boršov nad Vltavou", "535401": "Bošilec", "551490": "Branišov", "536059": "Břehov" } ...

What is the best way to organize divs in a grid layout that adapts to different screen sizes, similar to the style

Is there a way to align multiple elements of varying heights against the top of a container, similar to what is seen on Wolfram's homepage? I noticed that they used a lot of JavaScript and absolute positioning in their code, but I'm wondering if ...

Implementing functionality to switch classes when clicking outside an element using Jquery

At first glance, the code snippet reveals a method to toggle the .main-navigation and apply a shadow overlay on the page simultaneously. However, my objective is to trigger the same action (closing the menu and removing the overlay) by clicking outside the ...

The HTML view is unable to display the CSS style due to a MIME-type error

I have recently developed a very simple Express app that is supposed to display a single view called home.html from the view directory. Although the home.html file is being shown, none of the CSS styles I added seem to be loading. The console is throwing t ...

Using V-for to iterate over a multi-dimensional array

As I venture into the world of vue.js, I find myself faced with the challenge of populating nested select elements using v-for. Despite scouring through various resources on this topic, I have been unable to determine a solution that fits my particular sce ...

Invalid web address entered

While attempting to incorporate a functionality that opens a new window to a specific link when clicking an icon, I encountered an issue where the click action redirects to the wrong URL. Instead of directing to the URL specified in its href attribute, it ...

The issue of Segmentation fault in C programming when using an array inside a

Trying to deepen my understanding of C, I am currently delving into writing and experimenting with various code snippets. The current one I am working on is causing me some frustration as I am unsure of what changes need to be made for it to function corre ...

A guide to sending multiple data using Ajax or Json in Javascript

Sorry for my confusion, but I'm unsure if this involves AJAX or JSON as I am not proficient in this language. However, my issue pertains to sending values from two listboxes: the year and the month. The year is being sent to data-basic-colm-ajax.php, ...

Establish custom zones for every grouping on the y axis

Can anyone help me with adding a striped table-like background to my chart? I want to have alternating colors for each y-axis block. Is there a way to dynamically set the regions for each y-axis block? Once I can do this, I can easily use CSS to achieve t ...

Touch screen scrolling has been observed to significantly reduce the speed of AJAX downloads in Chrome

There is an unusual issue I am experiencing with an AJAX request that hangs intermittently when scrolling with a finger on a touch-enabled device. The problem occurs as long as the finger is held down while scrolling. It's difficult to explain the pro ...

How to create a custom hover effect for IconButtons in Material-UI

Customizing Hover Effects on IconButton I am currently using an IconButton component from Material-UI and have noticed a subtle grey border that appears when hovering over the icon. I am looking for a way to disable this hover effect, as I cannot seem to ...

Samsung Galaxy S7 can interpret alphabetical parameters as numbers in a link sent via SMS

When trying to open a text message with a new message on my website using a link, I encountered an issue specifically with the Galaxy S7. The following code works on most Android phones: sms:5555555555?body=JOIN However, on the Galaxy S7, the "?body=JOIN ...