{li}之间有

时间:2019-02-19 07:28:40

标签: javascript

嗨,我正在尝试使用DOM显示数组。问题是我在LI元素之间得到','。我该如何删除?

const boresult = {
  success: ["max-length", "no-amd", "prefer-arrow-function"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["id-blacklist", "no-dup-keys"]
};

function makeList(arr) {
  const resultDisplayArray = [];
  for (let i = 0; i < arr.length; i++) {
    resultDisplayArray.push(`<li>${arr[i]}</li>`);
    // resultDisplayArray.push('<li style="color: red">' + arr[i] + '</li>');
  }
  return resultDisplayArray;
}

// document.getElementById('ba').innerHTML = makeList(boresult.success);
const resultDisplayArray = makeList(boresult.success);
document.getElementById('ba').innerHTML = resultDisplayArray;

这是屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用空字符串将数组连接起来。

如果仅采用数组,并且环境期望使用字符串,则将调用toString方法,该方法与Array#join基本上相同,但是它采用的默认字符串为{{1} }。

为防止这种情况,您需要使用自己的参数。

,
function makeList(array) {
    return array.map(v => `<li>${v}</li>`);
}

const boresult = { success: ["max-length", "no-amd", "prefer-arrow-function"], failure: ["no-var", "var-on-top", "linebreak"], skipped: ["id-blacklist", "no-dup-keys"] };

document.getElementById('ba').innerHTML = makeList(boresult.success).join('');