Javascript 如何打印JSON获取文件的每个索引?

Javascript 如何打印JSON获取文件的每个索引?,javascript,arrays,json,foreach,vanilla-typescript,Javascript,Arrays,Json,Foreach,Vanilla Typescript,我有一个正在显示的表列表,如图中所示: 红色包围的问题是,我想将每个JSON数组的索引显示为我的表号 这是我的密码: function getExternal() { fetch('https://kyoala-api-dev.firebaseapp.com/queue-group/5e866db4-65f6-4ef0-af62-b6944ff029e5') .then(res => res.json()) .then((res) => {

我有一个正在显示的表列表,如图中所示:

红色包围的问题是,我想将每个JSON数组的索引显示为我的表号

这是我的密码:

function getExternal() {
   fetch('https://kyoala-api-dev.firebaseapp.com/queue-group/5e866db4-65f6-4ef0-af62-b6944ff029e5')
      .then(res => res.json())
      .then((res) => {
         let reservationList = '';

         res.forEach(function (reservation) {
            reservationList += `
            <tr>
               <th scope="row">Table</th>
               <td class="text-center">${reservation.minCapacity} - ${reservation.maxCapacity}</td>
               <td class="text-center">${reservation.activeQueuesCount}</td>
            </tr>`;
         });
         document.getElementById('lists').innerHTML = reservationList;
      })
      .catch(err => console.log(err));
};
函数getExternal(){ 取('https://kyoala-api-dev.firebaseapp.com/queue-group/5e866db4-65f6-4ef0-af62-b6944ff029e5') .then(res=>res.json()) 。然后((res)=>{ 让reservationList=''; res.forEach(功能(预订){ reservationList+=` 桌子 ${reservation.minCapacity}-${reservation.maxCapacity} ${reservation.activequeuescont} `; }); document.getElementById('lists').innerHTML=reservationList; }) .catch(err=>console.log(err)); };
基本上,您需要使用
forEach
功能中的
索引
,它将为您提供当前项目的
索引

在下面添加了更改:

function getExternal() {
   fetch('https://kyoala-api-dev.firebaseapp.com/queue-group/5e866db4-65f6-4ef0-af62-b6944ff029e5')
      .then(res => res.json())
      .then((res) => {
         let reservationList = '';

         res.forEach(function (reservation, index) {
            reservationList += `
            <tr>
               <th scope="row">${index}</th>
               <td class="text-center">${reservation.minCapacity} - ${reservation.maxCapacity}</td>
               <td class="text-center">${reservation.activeQueuesCount}</td>
            </tr>`;
         });
         document.getElementById('lists').innerHTML = reservationList;
      })
      .catch(err => console.log(err));
};
函数getExternal(){ 取('https://kyoala-api-dev.firebaseapp.com/queue-group/5e866db4-65f6-4ef0-af62-b6944ff029e5') .then(res=>res.json()) 。然后((res)=>{ 让reservationList=''; res.forEach(函数(保留、索引){ reservationList+=` ${index} ${reservation.minCapacity}-${reservation.maxCapacity} ${reservation.activequeuescont} `; }); document.getElementById('lists').innerHTML=reservationList; }) .catch(err=>console.log(err)); };
forEach迭代器为您提供(保留、索引),只需更新以包含索引。谢谢您,先生。。。。所以我的代码有点错误,我试过你在函数部分所做的,比如:函数(索引,保留),所以保留必须是第一个参数,然后索引将是下一个参数,就像你所做的那样。谢谢。