将对象添加到先前缺少的数组 - 角度2+ /离子2+

时间:2017-10-05 05:33:52

标签: angular ionic-framework ionic2 ionic3

我有一个数组和一个对象。该阵列是单个api调用。我每次发送ID时都会调用该对象。在我的旧数组中有一个缺少的参数 - 数据。我需要通过匹配imageID并查看哪个对象没有数据对象来将数据对象添加到此数组,假设通过将其过滤掉然后从那里映射它们。问题是我在过滤旧数组时遇到问题,只能对没有数据的对象进行调用。

[
{itemID: 10, color: "blue", imageID: "1", data: "somedata"}
{itemID: 11, color: "red", imageID: "2"}
{itemID: 12, color: "green", imageID: "2", data: "somedata2"}
{itemID: 13, color: "green", imageID: "1", data: "somedata"}
]

新 - 每个对象都是一个api调用。它将旧数组的imageID考虑在内。

{imageID: "1", data: "somedata"}
{imageID: "2", data: "somedata2"}

我想要的是什么:

[
{itemID: 10, color: "blue", imageID: "1", data: "somedata"}
{itemID: 11, color: "red", imageID: "2", data: "somedata2"}
{itemID: 12, color: "green", imageID: "2", data: "somedata2"}
{itemID: 13, color: "green", imageID: "1", data: "somedata"}
]

Component.ts

this.oldArray.map(res => {

    this.new(res)

  })

new(res){
    this.service.new(res.imageID).subscribe(icon => {
        res.data = 'data:image/png;base64,' + icon.data;
    }
   )
  }

1 个答案:

答案 0 :(得分:1)

试试这样:

ngOnInit() {
    this.oldArray.map(obj => {
        this.news(obj);
    })
}

news(obj) {
    let newObj = { imageID: "2", data: "somedata2" }
    if (!obj.data) {
        if (obj.imageID === newObj.imageID) {
            obj.data = newObj.data;
            console.log('obj', obj);
        }
    }
}