使用 forEach 方法使用模板文字创建字符串

发布于 2025-01-10 04:04:34 字数 1100 浏览 0 评论 0原文

有人可以帮我修复我的代码吗?我在这个练习中使用了 forEach 方法,

我认为我已经接近了,但它仍然给我未定义的结果,它已经在 result.failure 中,因为我通过添加另一个字符串/数字进行了检查。

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak",],
  skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
  // Only change code below this line
  const failureItems = [];

  const results = arr.forEach(k => failureItems.push(`<li class="text-warning">${arr[k]}</li>`));


  // Only change code above this line

  return failureItems;
}

const failuresList = makeList(result.failure);
console.log(failuresList);

应该是这样的结果

[
  '<li class="text-warning">no-var</li>',
  '<li class="text-warning">var-on-top</li>',
  '<li class="text-warning">linebreak</li>'
]

,但在这段代码中我的结果是这样的

[ '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>' ]

Can someone help me please fix my code? I was using forEach method in this exercise

I think I’m close but it’s still giving me undefined, it’s already inside the result.failure because I checked by adding another string/num.

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak",],
  skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
  // Only change code below this line
  const failureItems = [];

  const results = arr.forEach(k => failureItems.push(`<li class="text-warning">${arr[k]}</li>`));


  // Only change code above this line

  return failureItems;
}

const failuresList = makeList(result.failure);
console.log(failuresList);

it should be resulted to this

[
  '<li class="text-warning">no-var</li>',
  '<li class="text-warning">var-on-top</li>',
  '<li class="text-warning">linebreak</li>'
]

but in this code my result is this

[ '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>' ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-01-17 04:04:35

您想要连接k。另外,正如评论中提到的, Array#forEach 返回未定义

要解决上述问题:

arr.forEach(k => failureItems.push(`<li class="text-warning">${k}</li>`));

使用 数组#map

function makeList(arr) {
  return arr.map(k => `<li class="text-warning">${k}</li>`);
}

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak",],
  skipped: ["no-extra-semi", "no-dup-keys"]
};

const failuresList = makeList(result.failure);

console.log(failuresList);

You want to concatenate k. Also as mentioned in the comments, Array#forEach returns undefined.

To fix the above:

arr.forEach(k => failureItems.push(`<li class="text-warning">${k}</li>`));

Another solution using Array#map:

function makeList(arr) {
  return arr.map(k => `<li class="text-warning">${k}</li>`);
}

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak",],
  skipped: ["no-extra-semi", "no-dup-keys"]
};

const failuresList = makeList(result.failure);

console.log(failuresList);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文