【问题标题】:How to combine 2 JSON files in Angular 8如何在 Angular 8 中合并 2 个 JSON 文件
【发布时间】:2019-12-05 13:39:39
【问题描述】:

在我当前的 Angular 8 项目中,我必须连接两个 JSON 文件。连接后,新文件应上传到服务器。 这些文件是通过两个 HTML 输入标签选择的。

<input id="file_1" name="file_1" type="file">
<input id="file_2" name="file_2" type="file">

<button buttonLabel="convert" (click)="postJson()"></button>

JSON 1 的开始:

{
    "result": 0,
    "deviceInfos": [{
            "deviceIndex": 1,
            "info": {
                "name": "Test1234",
                "type": "Test",
                "serialNumber": 123456,
                "canBus": 0,
                "canPos": 1,
                .....

JSON 2 的开始:

{
    "result": 0,
    "deviceDescriptions": [
    {
            "deviceIndex": 1,
            "deviceTypeId": 34567,
            "descriptions": [{
                    "name": "Temp.Test",
                    "unitName": "",
                    "dataType": "string",
                    "constraints": {
                        "maxLen": 40,
                        "regExpr": "^([-_ a-zA-Z0-9]{0,40})$"
                    },
                    ......

我的实际组件:

export class JsonConverterComponent implements OnInit {

  constructor(private http: HttpClient) { }

  ngOnInit() {

  }

  postJson() {
    this.http.post(url, jarray)
      .subscribe(res => 
        console.log(res.json(

        ))
      );
  }

}

我不知道如何在单击按钮后访问这两个文件然后合并它们。

我该怎么做?

【问题讨论】:

  • 你试过解决方案了吗?
  • 是的,它工作正常。谢谢

标签: arrays json angular angular8


【解决方案1】:

试试这样:

this.result = this.json1.deviceInfos.flatMap(item => ({
  ...item,
  description : this.json2.deviceDescriptions.filter( x => x.deviceIndex == item.deviceIndex)
}));

Working Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多