使用 forEach 方法使用模板文字创建字符串
有人可以帮我修复我的代码吗?我在这个练习中使用了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要连接
k
。另外,正如评论中提到的,Array#forEach
返回未定义
。要解决上述问题:
使用
数组#map
:You want to concatenate
k
. Also as mentioned in the comments,Array#forEach
returnsundefined
.To fix the above:
Another solution using
Array#map
: