-1

I am new to angular. I want to convert this array to JSON data and want to use it in my component. I tried using some methods mentioned in stackoverflow, but they didn't work for me.

projects = [
        {
            name: 'Project1',
            day1: 4.22,
            day2: 3.56,
            day3: 3,
            day4: 1,
            day5: 7.8,
        },
        {
            name: 'Project2',
            day1: 1,
            day2: 5,
            day3: 2.5,
            day4: 4,
            day5: 1.9,
        },
        {
            name: 'Project3',
            day1: 6.78,
            day2: 2.55,
            day3: 4,
            day4: 3,
            day5: 1,
        },
    ]
5
  • What do you mean you want to convert to JSON data? Save it as JSON file and use/import it somewhere else in your app or simply use it as object? Commented Apr 30, 2020 at 14:09
  • Can u create stackblitz how to use data? U can use this object in your component. What do u want to change Commented Apr 30, 2020 at 14:09
  • There is no such thing as a Json data in Javascript. There are objects and arrays. What you have here is already an array of objects. You don't need any conversions. Loop through them using *ngFor directive and access the properties directly. Commented Apr 30, 2020 at 14:09
  • to use this array of json in .html file use ngFor and to use in .ts file use for loop Commented Apr 30, 2020 at 14:10
  • Does this answer your question? How to print the json array values in a list using *ngFor in Angular 2? Commented Apr 30, 2020 at 14:11

3 Answers 3

0

What you actually want??? If you Want to stringify it then try like this..... var myJsonString = JSON.stringify(yourArray);

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

Comments

0

Json arrays are already javascript arrays. You can simply create a property in an angular component and use it in a template, perhaps with *ngFor, since it is an array in this case.

Your component:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  projects = [
    { name: 'Project1', day1: 4.22, day2: 3.56, day3: 3, day4: 1, day5: 7.8, },
    { name: 'Project2', day1: 1, day2: 5, day3: 2.5, day4: 4, day5: 1.9, },
    { name: 'Project3', day1: 6.78, day2: 2.55, day3: 4, day4: 3, day5: 1, }
    ];
}

Your template:

<p *ngFor="let project of projects">
    {{ project.name }} | {{project.day1}} | {{project.day2}} | {{project.day2}}
</p>

Angular snippet

Comments

0

If you want to simply manipulate/use the array as object, you do not need to convert or save to JSON. You can access and us it simply as the following in your component:

const projects = [
        {
            name: 'Project1',
            day1: 4.22,
            day2: 3.56,
            day3: 3,
            day4: 1,
            day5: 7.8,
        },
        {
            name: 'Project2',
            day1: 1,
            day2: 5,
            day3: 2.5,
            day4: 4,
            day5: 1.9,
        },
        {
            name: 'Project3',
            day1: 6.78,
            day2: 2.55,
            day3: 4,
            day4: 3,
            day5: 1,
        },
    ]

 projects.map(p => console.log('Current project: ', p.name));

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.