0

I have array from local json file test.json

{"collection":[{"scriptId":"28936862","startDate":"2020-03-31T15:54:45.658992","endDate":"2020-03-31T15:57:33.573312","createDate":"31.03.2020 15:54:45"}}]

So this is my app.component.ts

import { Component } from '@angular/core';
import tasksData from '../assets/test.json';

interface Task {  
  scriptId: String;  
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {      
  tasks: Task[] = tasksData;
}

This is my app.component.html

<div class="container">
 <table class="table table-striped">
   <thead>
    <tr>        
     <th>Name</th>        
    </tr>
  </thead>
 <tbody>
  <tr *ngFor="let task of tasks">
    <td>{{ task.scriptId }}</td>        
  </tr>
 </tbody>
</table>

The code above i took from here. In the example array is simple. But my array starts with "collection", and the code doesn't work.

My IDE gives an error on this line tasks: Task[] = tasksData;

How can I change the code for my json array?

Thank you in advance

1
  • Try this - var data = JSON.parse('{"collection":[{"scriptId":"28936862","startDate":"2020-03-31T15:54:45.658992","endDate":"2020-03-31T15:57:33.573312","createDate":"31.03.2020 15:54:45"}]}'); var taskList = [...data.collection]; Commented Jul 17, 2020 at 12:41

1 Answer 1

2

Define tasks property like this :

tasks: Task[] = tasksData.collection;

and your interface should be like :

interface Task {  
  scriptId: string;
  startDate: string;
  endDate: string;
  createDate: string;
}
Sign up to request clarification or add additional context in comments.

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.