Home > Blockchain >  Get data from object in angular
Get data from object in angular

Time:08-16

I got this code from backend 10:34. My Object looks like

"StatusText": {

    "10": {
             "add": {
                  "34": {
                       "de": "Adding Text",
                       "en": "Adding Text"
                       }
                    },
             "de": "Main text",
             "en": "Main Text"
    }
},

How could I get text from Adding Text in Typescript?

My idea is I will split the code to 2 parts. The first part should be shown the main text and the second part should be shown the adding text.

What I tried:

const statusText = fetched.data[0].StatusText;
const split = statusCode.split(':');
    const mainStatus = statusText[split[0]];
    const addedStatus = statusText[split[1]];
  
    let mainStatusText = mainStatus.en;
    let addedStatusText = addedStatus.en;

CodePudding user response:

You question is not clear at all.

If you want to access the "Adding Text" string, you must convert the response from JSON then access it like:

data.StatusText.10.add.34.de

CodePudding user response:

i don't see a relation with angular this is a javascript. this is a good post from mozilla developer documentation if you want to learn how to work with objects.

Now let me back to your example. if i see your code i don't see why you are splitting your object , you can access to your property directly using this syntax :

data[property][property2]...

in your case you can use this :

let mainStatusText = fetched.data[0].StatusText['10']['en'];
let addedStatusText = fetched.data[0].StatusText['10']['add']['34']['en'];

you can replace ['x'] by a simple '.x'

  • Related