Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

两个数组和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
}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...