1. 程式人生 > 其它 >JS 遍歷獲取陣列中的陣列的值

JS 遍歷獲取陣列中的陣列的值

//檔案結構
var listInfo = [ { zTit: '網站首頁', icon: './image/left.png', hicon: './image/down.png', xTit: [ { name: '關於我們', href:'./home.html', }, { name: '客戶中心', href:'./home.html', }, { name: '新聞資訊', href:'./home.html', } ], }, { zTit: '網站首頁', icon: './image/left.png', hicon: './image/down.png', xTit: [ { name: '關於我們', href:'./home.html', }, { name: '客戶中心', href:'./home.html', }, { name: '新聞資訊', href:'./home.html', } ], }, ]

//用forEach方法遍歷獲取

listInfo.forEach((item, index) => { selects.innerHTML += ` <li class="selects_list"> <a></a> <div class="h_list_con"> <p>${item.zTit}</p> <div class="icon"> <img class="sIcon" src="${item.icon}" alt=""> <img class="hIcon" style="display:none;" src="${item.hicon}" alt=""> </div> </div> <div class="h_list_hid"> ${item.xTit.map((element, index) => { return `<p><a href="${element.href}">${element.name}</a></p>` }).join('')} </div> </li> ` })