Javascript:使用另一个数组中的对象映射数组并返回计算出的数组

时间:2019-03-29 01:23:09

标签: javascript vue.js

这看似微不足道,但我很难弄清楚这一点。

我有一个如下数组:
数组1:

[
    {
        "id": 1,
        "date": "2019-03-27",
        "time": 1,
        "max_tasks": 3,
        "reservations": [
            5,
            2
        ]
    },
    {
        "id": 2,
        "date": "2019-03-28",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 3,
        "date": "2019-03-29",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 4,
        "date": "2019-03-30",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 5,
        "date": "2019-03-31",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 6,
        "date": "2019-04-01",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 7,
        "date": "2019-04-02",
        "time": 1,
        "max_tasks": 3,
        "reservations": [
            3
        ]
    }

]

这里reservations包含我需要使用另一个数组的ID,如下所示:

数组2:

[
    {
        "id": 5,
        "app": 1,
        "comment": "test 5"
    },
    {
        "id": 2,
        "app": 1,
        "comment": "test 2"
    }
]

预期:

我需要一种将数组2的数据包含在数组1中的方法,在这里我使用数组1的id的{​​{1}}并从数组2中提取相应的对象。最后,我需要返回数组1这些对象是从Array 2添加的。

我正在使用Vue,希望在getters中计算该数组,以便可以在我的组件中获取并直接映射到那里。如果有更好的方法,我欢迎任何建议

这就是我要获取的输出:

`

reservations

`

感谢您的时间。

3 个答案:

答案 0 :(得分:0)

也许可行

var arr1 = [
    {
        "id": 1,
        "date": "2019-03-27",
        "time": 1,
        "max_tasks": 3,
        "reservations": [
            5,
            2
        ]
    },
    {
        "id": 2,
        "date": "2019-03-28",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 3,
        "date": "2019-03-29",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 4,
        "date": "2019-03-30",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 5,
        "date": "2019-03-31",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 6,
        "date": "2019-04-01",
        "time": 1,
        "max_tasks": 3,
        "reservations": []
    },
    {
        "id": 7,
        "date": "2019-04-02",
        "time": 1,
        "max_tasks": 3,
        "reservations": [
            3
        ]
    }

]

var arr2 = [
    {
        "id": 5,
        "app": 1,
        "comment": "test 5"
    },
    {
        "id": 2,
        "app": 1,
        "comment": "test 2"
    }
]

for (var i = 0; i != arr1.length; ++i) {
    arr1[i].reservations = arr1[i].reservations.map(id => {
        var reservation = arr2.find(reservation => reservation.id == id)
        return reservation || id;
    });
}

它将尝试在数组2中查找保留,否则将不会替换ID。

答案 1 :(得分:0)

这可能不是最佳解决方案,但请尝试一下。

let arr1 = [{
    "id": 1,
    "date": "2019-03-27",
    "time": 1,
    "max_tasks": 3,
    "reservations": [
      5,
      2
    ]
  },
  {
    "id": 2,
    "date": "2019-03-28",
    "time": 1,
    "max_tasks": 3,
    "reservations": []
  },
  {
    "id": 3,
    "date": "2019-03-29",
    "time": 1,
    "max_tasks": 3,
    "reservations": []
  },
  {
    "id": 4,
    "date": "2019-03-30",
    "time": 1,
    "max_tasks": 3,
    "reservations": []
  },
  {
    "id": 5,
    "date": "2019-03-31",
    "time": 1,
    "max_tasks": 3,
    "reservations": []
  },
  {
    "id": 6,
    "date": "2019-04-01",
    "time": 1,
    "max_tasks": 3,
    "reservations": []
  },
  {
    "id": 7,
    "date": "2019-04-02",
    "time": 1,
    "max_tasks": 3,
    "reservations": [
      3
    ]
  }

]
let arr2 = [{
    "id": 5,
    "app": 1,
    "comment": "test 5"
  },
  {
    "id": 2,
    "app": 1,
    "comment": "test 2"
  }
]
arr1.forEach(o => {
  let updatedReverations = []
  o.reservations.forEach(r => {
    updatedReverations.push(arr2.filter(a2 => r === a2.id)[0] || r)
  })
  o.reservations = updatedReverations
})
console.log(arr1)

答案 2 :(得分:0)

下面的功能将为您提供合并数组的新副本。

function getMergedArray(arr1,arr2){
return arr1.map(item=>{
    if(item.reservations.length>0){
     let newReservations= item.reservations.map(reservationId=>{
            let foundReservation = arr2.find(reservation=>{
                return reservation.id === reservationId
        })
        if(foundReservation){
            return foundReservation;
        }
        else{
            return reservationId;
        }
     })
     item.reservations = newReservations;
  }

  return item;
});

}