首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将forEach循环输出到innerHTML

问将forEach循环输出到innerHTML
EN

Stack Overflow用户
提问于 2018-07-24 00:04:08
回答 4查看 6.5K关注 0票数 1

愚蠢的问题,适应使用ES6 / Vanilla JS。

这是一个与console.log完美配合的循环……

​

代码语言:javascript
复制
const theServices = 
      ["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


theServices.forEach(function(element) {
    let formName = element;
    formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

console.log( `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`)
});
代码语言:javascript
复制
<div id="place-here"></div>

​

现在,尝试使用innerHTML插入DOM时返回未定义的结果。像这样输出循环的合适方式是什么?

​

代码语言:javascript
复制
const theServices = 
      ["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


let theseCheckBoxes =
    theServices.forEach(function(element) {
        let formName = element;
        formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

        return `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`
    });
;

document.querySelector('#place_here').innerHTML = theseCheckBoxes;
代码语言:javascript
复制
<div id="place_here"></div>

​

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-07-24 00:07:25

ForEach不返回任何内容。所以theseCheckBoxes将是未定义的。尝试将其更改为map()并对结果执行join()。

​

代码语言:javascript
复制
const theServices = 
      ["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


let theseCheckBoxes =
    theServices.map(function(element) {   // <-- map instead of forEach
        let formName = element;
        formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

        return `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`
    });
;

document.querySelector('#place_here').innerHTML = theseCheckBoxes.join('\n');
代码语言:javascript
复制
<div id="place_here"></div>

​

票数 4
EN

Stack Overflow用户

发布于 2018-07-24 00:07:37

您不能从forEach执行return a value,但您可以在迭代时构建html字符串,或者使用map。

​

代码语言:javascript
复制
const theServices = 
      ["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


let theseCheckBoxes = "";
    theServices.forEach(function(element) {
        let formName = element;
        formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

        theseCheckBoxes +=`
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`
    });
;

document.querySelector('#place_here').innerHTML = theseCheckBoxes;
代码语言:javascript
复制
<div id="place_here"></div>

​

票数 3
EN

Stack Overflow用户

发布于 2018-07-24 00:31:28

我想添加另一个解决方案,那就是使用reduce将数组缩减为如下所示的字符串:

​

代码语言:javascript
复制
const theServices = ["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];

let str = theServices.reduce(function(str, element) {
  let formName = element;
  formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

  return str.concat(`<div>
            <input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
            <label for="${formName}">${element}</label>
          </div>`)
}, '');

document.querySelector('#place_here').innerHTML = str;
代码语言:javascript
复制
<div id="place_here"></div>

​

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51483027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档