【问题标题】:Merge the object using typescript使用打字稿合并对象
【发布时间】:2019-04-20 22:35:40
【问题描述】:

在我的角度应用程序中,我的数据如下,

  forEachArrayOne = [
    { id: 1, name: "userOne" },
    { id: 2, name: "userTwo" },
    { id: 3, name: "userThree" }
  ]

  forEachArrayTwo = [
    { id: 1, name: "userFour" },
    { id: 2, name: "userFive" },
    { id: 3, name: "userSix" }
  ]

  newObj: any = {};

  ngOnInit() {
this.forEachArrayOne.forEach(element => {
  this.newObj = { titleOne: "objectOne", dataOne: this.forEachArrayOne };
})

this.forEachArrayTwo.forEach(element => {
  this.newObj = { titleTwo: "objectTwo", dataTwo: this.forEachArrayTwo };
})

console.log({ ...this.newObj, ...this.newObj });


  }

在我的实际应用中,以上是结构,所以请帮助我以同样的方式达到预期的结果..

工作演示https://stackblitz.com/edit/angular-gyched具有上述结构。

这里console.log(this.newObj)给出了最后一个对象,

   titleTwo: "ObjectTwo",
   dataTwo:
     [
       { id: 1, name: "userFour" },
      { id: 2, name: "userFive" },
      { id: 3, name: "userSix" }
     ]

但我想将两者结合起来,并且需要与下面完全一样的结果..

  {
  titleOne: "objectOne", 
  dataOne:
    [
      { id: 1, name: "userOne" },
      { id: 2, name: "userTwo" },
      { id: 3, name: "userThree" }
    ],
  titleTwo: "ObjectTwo",
  dataTwo:
    [
      { id: 1, name: "userFour" },
      { id: 2, name: "userFive" },
      { id: 3, name: "userSix" }
    ]
}

请帮助我实现上述结果..如果我在任何地方有错误,请通过工作示例更正..

【问题讨论】:

    标签: javascript angular typescript object merge


    【解决方案1】:

    您将这两个值都分配给this.newObj,因此它只会覆盖第一个对象。

    另外,也不需要你的循环。它没有添加任何东西。

    相反,您可以这样做:

    this.newObjA = { titleOne: "objectOne", dataOne: this.forEachArrayOne };
    this.newObjB = { titleTwo: "objectTwo", dataTwo: this.forEachArrayTwo };
    console.log({ ...this.newObjA, ...this.newObjB });
    

    ** 编辑 **

    在与您讨论过您的要求后,我可以看到不同的解决方案。

    在致电componentData 之前,您需要确保您拥有完整的数据。为此,我们可以使用forkJoin 加入基准请求,并将项目请求合并为一个Observable。然后,我们可以订阅 Observable 以获取两者的结果。

    代码如下所示:

     createComponent() {
        let benchmarks, projects;
        let form = this.productBenchMarkingForm[0];
        if (form.benchmarking && form.project) {
          benchmarks = form.benchmarking.filter(x => x.optionsUrl)
            .map(element => this.getOptions(element));
    
          projects = form.project.filter(x => x.optionsUrl)
            .map(element => this.getOptions(element));
    
          forkJoin(
            forkJoin(benchmarks), // Join all the benchmark requests into 1 Observable
            forkJoin(projects) // Join all the project requests into 1 Observable
          ).subscribe(res => {
            this.componentData({ component: NgiProductComponent, inputs: { config: AppConfig, injectData: { action: "add", titleProject: "project", dataProject: this.productBenchMarkingForm[0] } } });
          })
        }
      }
    
      getOptions(element) {
        return this.appService.getRest(element.optionsUrl).pipe(
          map((res: any) => {
            this.dataForOptions = res.data;
            element.options = res.data;
            return element;
          })
        )
      }
    

    Here is an example in Stackblitz that logs the data to the console

    【讨论】:

    • 但在我的项目中,我只有这样的结构。所以只有我用 foreach 解释过。在我的问题中也提到了我正在使用的结构。
    • 我也知道这个解决方案,但我的情况不是这样。我在我的问题中编写代码......
    • 好的,目前你的问题没有提供足够的信息让我回答。你能给出你的数据的实际结构吗?那么也许我可以理解你为什么需要使用循环。
    • 如果不是这个可能,请帮助我如何更正上面的代码。因为我将newObj 传递给另一个组件,所以我需要将两个单独的值分配给@987654330 @ 并进入该组件..
    • 我已经更新了这个问题。我正在加载另一个组件,所以我正在通过componentData 发送数据,我在组件二内的this.injectedDataFromComponentOne 中获取它。但事情是我就像我在我的问题中解释的那样,我在那里单独获得第二个对象值..
    猜你喜欢
    • 2018-09-15
    • 2019-07-17
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多