如何使用for循环用多个数组数据填充内部html?

时间:2019-03-25 12:58:12

标签: typescript

我有4个数据数组,我希望将它们放入for循环中以填充元素的内部html或使用来自数组的不同迭代数据生成相同的元素。

document.getElementById(“ test”)。innerHTML =(driverPosition +“” + nameLastName +“” + driverSponsor +“ + + driverPoints);

在有效的循环中,但是它只返回最后一个值,我希望它返回所有值都比另一个低。

    let driverPositionArray = [];
    let nameLastNameDriver = [];
    let listaSponsora = [];
    let driverPointsArray = [];
    let i = 0;

 for (; i < duzinaNumber; i++) {
      nameVozaca = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings[i].Driver.givenName;
      prezimeVozaca = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings[i].Driver.driverId;
      // console.log("name + last name"+ " " + nameVozaca + " " + prezimeVozaca);
      driverPosition = (response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings[i].position);
      // console.log("this is the position of the current driver " + driverPosition);
      driverPoints = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings[i].points;
      // console.log("current driver points " + driverPoints);
      // console.log("this is iteration number" +" "+ i);

      driverSponsor = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings[i].Constructors[0].name;
      //  console.log(driverSponsor); radi ok
      //   console.log( "position " + driverPosition +  " ime Vozaca " + nameVozaca + " prezime Vozaca " + prezimeVozaca + " " + " Points " + driverPoints);

      driverPositionArray.push(driverPosition);
      nameLastName = (nameVozaca + " " + prezimeVozaca);  // vraca string imena i prezimena u array listu
      nameLastNameDriver.push(nameLastName);
      listaSponsora.push(driverSponsor);
      driverPointsArray.push(driverPoints);
      //console.log(nameLastName);
      //  imenaVozaca.push(nameVozaca);
    }

}

我想要一个for循环,该循环从4个数组中获取数据,并将它们填充到p标签中的html中。enter image description here

1 个答案:

答案 0 :(得分:0)

document.getElementById(“ test”)。innerHTML + =“” + driverPosition +“” + nameLastName +“” + driverSponsor +“” + driverPoints +“
”;