gpt4 book ai didi

Javascript forEach 返回值插入到字符串中

转载 作者:行者123 更新时间:2023-11-30 12:12:50 24 4
gpt4 key购买 nike

stack_html += "<div class='co-stack-layer-title'>Application and Data" +
"<div class='row'>" +
response['Application and Data'].forEach(generateStackItem) +
"</div>" +
"</div>";
stack_html += "<div class='co-stack-layer-title'>Business Tools" +
"<div class='row'>" +
response['Business Tools'].forEach(generateStackItem) +
"</div>" +
"</div>";
stack_html += "<div class='co-stack-layer-title'>DevOps" +
"<div class='row'>" +
response['DevOps'].forEach(generateStackItem) +
"</div>" +
"</div>";
stack_html += "<div class='co-stack-layer-title'>Utilities" +
"<div class='row'>" +
response['Utilities'].forEach(generateStackItem) +
"</div>" +
"</div>";

function generateStackItem(element, index, array) {
var stack_item_html = "";
stack_item_html += "<div class='co-service'>" +
"<a href='" + element['canonical_url'] + "' class='each-service hint hint--top' data-hint='" + element['name'] + "' data-align='left'>" +
"<img src='" + element['image_url'] + "'>" +
"</a>" +
"</div>";

console.log(stack_item_html);
return stack_item_html;
}

我正在尝试对 javascript 对象使用 forEach 迭代,但似乎无法将返回值插入到字符串添加中。

<div class='co-stack-layer-title'>Application and Data<div class='row'>undefined</div></div><div class='co-stack-layer-title'>Business Tools<div class='row'>undefined</div></div><div class='co-stack-layer-title'>DevOps<div class='row'>undefined</div></div><div class='co-stack-layer-title'>Utilities<div class='row'>undefined</div></div>

最佳答案

forEach返回 undefined。使用 map +加入

"abc" + response['Utilities'].map(generateStackItem).join("") + "def";

关于Javascript forEach 返回值插入到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33285348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com