两个数组和1个对象合并成一个数组对象的排列组合


const userIds = [1, 2]
const dates = ["2021-01-09", "2021-01-10"]
const obj = {
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
}

最终结果

[{
  "user_id": 1,
  "date":"2021-01-09"
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
},{
  "user_id": 1,
  "date":"2021-01-10"
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
},{
  "user_id": 2,
  "date":"2021-01-09"
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
},{
  "user_id": 2,
  "date":"2021-01-10"
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
}]
阅读 1.6k
2 个回答

image.png

var userIds = [1, 2]
var dates = ["2021-01-09", "2021-01-10"]
var obj = {
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
}
userIds.map(user=>{
    return dates.map(date=>{
        return Object.assign({
            user_id: user,
            date: date,
        },obj)
    })
}).flat()
新手上路,请多包涵
var userIds = [1, 2]
var dates = ["2021-01-09", "2021-01-10"]
var obj = {
  "time_from": "09:00:00",
  "time_to": "20:00:00",
  "rest_hours": 1
}
userIds.map(user=>{
    return dates.map(date=>{
        return Object.assign({
            user_id: user,
            date: date,
        },obj)
    })
}).flat()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏