将 JSON 查询的字段写入文件 Angular

Writing a field of a JSON query to a file Angular

我正在编写一个 Angular 应用程序,它以 JSON 格式从 API 中获取数据,并将 JSON 结果中的特定字段写入文件。例如,如果查询结果为

[{"id":1,"name":"Banana"},{"id":2,"name":"Apple"}, {"id":3,"name":"Orange"},{"id":4,"name":"Kiwi"},{"id":5,"name":"Strawberry"},{"id":6,"name":"Blueberry"},{"id":7,"name":"Tomato"}]

我只想将名称字段放入文本文件中,如下所示:

Banana

Apple

Orange

Kiwi

Strawberry

Blueberry

Tomato

我该怎么做?

我了解如何查询 API 并将整个查询写入文本文件: 在我的 TypeScript 文件中:

this.http.get(this.url, httpOptions).subscribe((response) => {
      this.response = response;
      const blob = new Blob([JSON.stringify(this.response)], { type: 'application/json' });
      this.fileURL = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob))
    });

在我的 HTML 文件中:

<a [href]="fileURL" download="name.txt">Download File</a>

但我一直坚持过滤数据并将其成功写入文件。我该怎么做?

Array filter

var names = [{"id":1,"name":"Banana"},{"id":2,"name":"Apple"}, {"id":3,"name":"Orange"},{"id":4,"name":"Kiwi"},{"id":5,"name":"Strawberry"},{"id":6,"name":"Blueberry"},{"id":7,"name":"Tomato"}].map(i => i.name);

console.log(names);

let result = [{ "id": 1, "name": "Banana" }, { "id": 2, "name": "Apple" }, { "id": 3, "name": "Orange" }, { "id": 4, "name": "Kiwi" }, { "id": 5, "name": "Strawberry" }, { "id": 6, "name": "Blueberry" }, { "id": 7, "name": "Tomato" }];
let textToPut="";
result.forEach(elt => {
    textToPut+= elt.name+"\n"; // add element name and back to line 
});
console.log(textToPut);
// Then put textToPut in your file