js中foreach嵌套遍历两个数组并输出新的数组

<script>
    let arrA=['1','2','3','4','5']
    let arrB =['2020-10-1','2020-10-2','2020-10-3']
    let arrC=[]
    arrA.forEach(item1=>{
        arrB.forEach(item2=>{
            let obj={}
            obj.shiftTime=item2
            obj.bedId=item1
            arrC.push(obj)
        })
    })
    console.log(arrC)
</script>

效果:

[
{
"shiftTime": "2020-10-1",
"bedId": "1"
},
{
"shiftTime": "2020-10-2",
"bedId": "1"
},
{
"shiftTime": "2020-10-3",
"bedId": "1"
},
{
"shiftTime": "2020-10-1",
"bedId": "2"
},
{
"shiftTime": "2020-10-2",
"bedId": "2"
},
{
"shiftTime": "2020-10-3",
"bedId": "2"
},
{
"shiftTime": "2020-10-1",
"bedId": "3"
},
{
"shiftTime": "2020-10-2",
"bedId": "3"
},
{
"shiftTime": "2020-10-3",
"bedId": "3"
},
{
"shiftTime": "2020-10-1",
"bedId": "4"
},
{
"shiftTime": "2020-10-2",
"bedId": "4"
},
{
"shiftTime": "2020-10-3",
"bedId": "4"
},
{
"shiftTime": "2020-10-1",
"bedId": "5"
},
{
"shiftTime": "2020-10-2",
"bedId": "5"
},
{
"shiftTime": "2020-10-3",
"bedId": "5"
}
]

posted @ 2022-11-11 21:50  quitpoison  阅读(992)  评论(0编辑  收藏  举报