What is the best way to extract data from an [object Object] and store it in an Array or access its values in Angular?

My Angular code is written in the component.ts file. I am fetching data from a backend API and using console.log to display the data.

getInfo() {
    const params = [];
       
    params.push({code: 'Code', name: 'ty_PSD'});
    params.push({code: 'continentCodes', name: ['CAN']});
    params.push({code: 'dateFrom', name: '2019-01-01'});
    params.push({code: 'dateTo', name: '2019-12-31'});
    params.push({code: 'statusType', name: 'REAL'});
    params.push({code: 'valueType', name: 'TTV'});
        this.serviceHandler.getDemand([], params).subscribe(
          demand => {       
            console.log(demand + 'ddd');
    
          });
      }

The output of console.log(demand) can be seen at https://i.stack.imgur.com/ScYxg.png. The API response is displayed as shown in https://i.stack.imgur.com/n63TN.png.

I'm trying to extract values into an array from the data: Array(365). How can I achieve this?

Answer №1

Avoid combining or adding an object with a string as console.log treats the object as a string.

The desired output for the examples below is the number 4:

console.log(JSON.stringify(obj) + 'aaa');

var obj = { a: 'aa', b: 0, c: true};

console.log(1, obj + 'aaa');
console.log(2, obj);
console.log(3, JSON.stringify(obj));
console.log(4, JSON.stringify(obj) + 'aaa');

Answer №2

Make sure to convert the API response from a string format into a readable format by parsing it.

Consider using this snippet:

const data = JSON.parse(response)
console.log(data)

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

Why is it that a character pointer array does not print the pointer itself, but instead prints the string it is pointing to?

I came across the following code snippet: static char *name[] = { "January", "February", "March", }; printf("%s", name[0]); Upon passing printf with name[0], it outpu ...

Leveraging TypeScript enums in conjunction with React to anticipate a string prop

Trying to enforce strict typing for a Button component in React. How can I ensure a prop has a specific string value? The current effort yields Type '"primary"' is not assignable to type 'ButtonLevel' enum ButtonLevel { Primary = ...

What is the best way to adjust the size of an Angular Material Slider?

I'm attempting to insert a slider into my program, but the slider is displaying as too lengthy for the designated space. I'm curious if there's a straightforward method of adjusting its length to fit better within the container. So far, I&a ...

Looping Angular Components are executed

I am currently developing an Angular application and encountering an issue with my navbar getting looped. The problem arises when I navigate to the /home route, causing the navbar.component.html components to duplicate and appear stacked on top of each oth ...

When attempting to run the Angular build using the Azure Event Hub JS SDK, deployment fails to execute successfully

Whenever I test the project on my local machine with ng serve -o everything functions properly. However, after building it (either in dev or prod) and deploying it to either an Azure web app or a local server on my computer, I encounter the following ...

View the file by printing the multidimensional array

Seeking assistance with displaying an array in a view file. Here is my code: @foreach($standings as $team_name => $points) <tr> <th scope="row">#</th> <td>{{$team_name}}</td> &l ...

Error: The jasmine framework is unable to locate the window object

Currently, I am testing a method that includes locking the orientation of the screen as one of its functionalities. However, when using Jasmine, I encountered an error at the following line: (<any>window).screen.orientation.lock('portrait&apos ...

Unable to perform Undo function in monaco editor

Currently in my Angular 7 project, I have integrated the Monaco editor for coding purposes. One issue I am facing is that when I make a change to the code and then press ctrl+z to undo it, the previous code is successfully restored. However, if I change th ...

Discover the perfect way to implement true lazyloading using NativeScript Angular tabs and BottomNavigation

Currently working on an app using nativescipt, I've successfully added BottomNavigation with lazy loading and Tab components in child pages. The code structure resembles the following: export const routes: Routes = [ { path: '', red ...

Unable to locate a suitable version for internal-slot@^1.0.3 at this time

I'm encountering an issue with the npm installation process. When attempting to run npm install on my Angular application, I received an error message that is shown in the image above : No matching version found for internal-slot@^1.0.3 I att ...

Trouble loading Styled Components in React Typescript with Webpack configuration

Hey there! I'm diving into the world of styled components for the first time, and I'm a bit lost on where I might have slipped up. I've got my webpack all sorted out and my .babelrc file in place. As I was going through the Styled Component ...

The npm package has been successfully installed, but VS Code is having trouble locating it

Currently, I am in the process of developing a simple npm package known as type-exception using TypeScript. After successful test runs and publication on NPM, I have been able to install it into another project (project B). However, upon importing it as a ...

Creating Angular components in *ngFor loop

I have set up multiple radio button groups by dynamically populating options using ngFor within a ngFor loop. categories:string[] = [category_1, ..., category_n]; options:string[] = [option_1, ..., option_n]; <fluent-radio-group *ngFor='let ca ...

Utilizing Express-sessions to generate a fresh session with each new request

I'm facing an issue with my express backend using express-sessions and Angular frontend. Every time the frontend makes a request, a new session is created by express-sessions. I suspect the problem lies in Angular not sending the cookie back, as I don ...

What is the process for setting a Type to a prop in React?

I have a main component: // DashboardComponent.tsx const DashboardComponent = () => { const {loading, error, data} = useQuery(resolvers.ReturnAllMovies); if (loading) return <p>loading</p>; if (error) return <p>Error! ${error.m ...

Tips for accurately defining the return type for querySelector(All) connections

I enjoy doing this particular task, ensuring the types are correct. const qs = document.querySelector.bind(document) as HTMLElementTagNameMap | null; const qsa = document.querySelectorAll.bind(document) as NodeListOf<any>; While hovering over query ...

Pressing a button will reset the Mat Radio Group's Mat Radio Buttons back to their default settings

I have a list of items that need to be rated using radio buttons. I utilized Mat radio group and Mat radio buttons for this purpose. In the scenario below, I need to rate games as good, bad, or none. Once the changes are implemented, I want all the radio b ...

There was an issue with the code: ERROR TypeError: Can't access the length of an undefined property in Angular 4

Encountering this particular issue: LeaderBoardComponent.html:27 ERROR TypeError: Cannot read property 'length' of undefined The error seems to be originating from the following HTML: <div *ngIf="leaderBoardTableData.length > 0"> With ...

Making changes to a single formControl in angular6 will cause the other formControl to be updated as

I have two select menus in my user interface with the same options. When I select an option in one menu, I want the other menu to display the same option, and vice versa. Currently, I am using the valueChanges method on the first formControl to update the ...

Experiencing 429 Too Many Requests error on Angular 7 while attempting to perform multiple file uploads

Whenever I attempt to upload a large number of files simultaneously, I encounter an issue. The API interface only allows for the submission of one file at a time, requiring me to call the service for each individual file. Currently, my code looks like thi ...