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"
}
]
本文来自博客园,作者:quitpoison,转载请注明原文链接:https://www.cnblogs.com/quitpoison/p/16882147.html