【发布时间】:2020-02-23 18:35:23
【问题描述】:
我正在开发一个新的 Angular 8 项目,在我的组件中,我从 2 个不同的服务获取 json 数据。 数据是对象数组。我想加入数组中的对象并将其发送回数据库。
这里是代码
import { Component , OnInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
// data comes from Persons Service
person = [
{name: "john", id: 1, joined: "2018"},
{name: "sarah", id: 2, joined: "2019"}
]
// data comes from salary Service
salary = [
{name: "john", id: 1, salary: 3000},
{name: "sarah", id: 2, salary: 5000}
]
personWithSalary;
ngOnInit(){
}
}
我如何将这些数据组合在一起并将工资映射到正确的人,以便它进入一个新的数组,如下所示:
personWithSalary = [
{name: "john", id: 1, joined: "2018", salary: 3000},
{name: "sarah", id: 2, joined: "2019", salary: 5000}
]
【问题讨论】:
-
但是,您的
salary已经拥有所需的数据 -
它有想要的数据,但是第一个数组比第二个数组有更多的属性,我只是为了指导而编了这个数据。
-
我已经编辑了这个问题,让它更有意义。
标签: arrays json angular object