三个数据一行的HTML拼接法

输出:

<ul>
    <li>Mac Pro</li>
    <li>华为P20</li>
    <li>小米手机</li>
</ul>
<ul>
    <li>玛莎拉蒂</li>
    <li>特斯拉Model S</li>
    <li>jackjones</li>
</ul>
<ul>
    <li>天生要强</li>
</ul>
     let data = [
        {
          name: "iPhone X",
          value: 19651,
          },
        {
          name: "Mac Pro",
          value: "接\n入\n情\n况",
          },
        {
          name: "华为P20",
          value: 17319,
          },
        {
          name: "小米手机",
          value: 12550,
          },
        {
          name: "欧莱雅",
          value: 11638,
          },
        {
          name: "玛莎拉蒂",
          value: 10608,
          },
        {
          name: "特斯拉Model S",
          value: 8298,
          },
        {
          name: "jackjones",
          value: 7630,
          },
        {
          name: "天生要强",
          value: 6065,
          },
          {
          name: "天生不强",
          value: 6065,
          },
      ];
      let ret = "";
      for (let idx = 0; idx < data.length; idx++) {
        if (idx % 4 === 0) {
          ret += "</ul><ul>";
        } else {
          ret += "<li>"+data[idx].name+"</li>";
        }
        if(idx===data.length-1){
          ret += "</ul>";
        }
      }
      ret = ret.replace(/<\/ul>/,'').replace('<ul></ul>','')
      console.log(ret);

 

posted @ 2021-10-11 11:02  洛晨随风  阅读(217)  评论(0编辑  收藏  举报