最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript forEach inside another function - Stack Overflow

programmeradmin2浏览0评论

INTRO: I am kinda new to JavaScript and need a little help. What I am trying to do is build is return a string of html, and I need to introduce some variables into a html table.

PROBLEM: I am trying to do forEach loop to collect data I would like to output. I can loop through and get the data to return just fine, however I have my forEach loop inside another function.

Code:

'<table>' +
      '<tr><th>Product Details:</th><th></th><th></th><th></th></tr>' +
     '<tr><th>Product</th><th>Manufacture Site</th><th>Business</th><th>Hazmat #</th><th>MSDS</th></tr>' +
     myFunction() +


  '</table>'...

Sorry that it's a little hard to read.

Function:

function myFunction() {
    productDetailsGrid.items.forEach(function(item) {
        var prod = item.data.Product;
        var sites= item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        var str = '<tr><th>' + prod + '</th><th>' + sites+ '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
        return str;
    });
}

So, since I receive the data, how can I return each str as a string to the string that calls it?

INTRO: I am kinda new to JavaScript and need a little help. What I am trying to do is build is return a string of html, and I need to introduce some variables into a html table.

PROBLEM: I am trying to do forEach loop to collect data I would like to output. I can loop through and get the data to return just fine, however I have my forEach loop inside another function.

Code:

'<table>' +
      '<tr><th>Product Details:</th><th></th><th></th><th></th></tr>' +
     '<tr><th>Product</th><th>Manufacture Site</th><th>Business</th><th>Hazmat #</th><th>MSDS</th></tr>' +
     myFunction() +


  '</table>'...

Sorry that it's a little hard to read.

Function:

function myFunction() {
    productDetailsGrid.items.forEach(function(item) {
        var prod = item.data.Product;
        var sites= item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        var str = '<tr><th>' + prod + '</th><th>' + sites+ '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
        return str;
    });
}

So, since I receive the data, how can I return each str as a string to the string that calls it?

Share Improve this question asked Oct 26, 2017 at 13:19 TigerDaveTigerDave 653 silver badges14 bronze badges 2
  • 2 place the var str = "" at the beginning of myFunction, then do -> str += ... inside your forEach, and then place return str inside you myFunction.. – Keith Commented Oct 26, 2017 at 13:22
  • foreach does make a return or break... – Álvaro Touzón Commented Oct 26, 2017 at 13:24
Add a ment  | 

4 Answers 4

Reset to default 3

Instead of forEach you can use map and join the resulting array:

function myFunction() {
    return productDetailsGrid.items.map(function (item) {
        var prod = item.data.Product;
        var sites = item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        return '<tr><th>' + prod + '</th><th>' + sites + '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
    }).join("");
}

You could just declare the variable outside of the .forEach like so

function myFunction() {
    var str = "";
    productDetailsGrid.items.forEach(function (item) {
        var prod = item.data.Product;
        var sites = item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        str += '<tr><th>' + prod + '</th><th>' + sites + '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
    });
    return str;
}

I would advice to use reduce for that purpose since that's what it is for.

function myFunction() {
    return productDetailsGrid.items.reduce(function(str, item) {
        var prod = item.data.Product;
        var sites= item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        str += '<tr><th>' + prod + '</th><th>' + sites+ '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
        return str;
    });
}

ncardeli's answer is already pretty good. However, if you want to get the strings as an array you can use this:

var items = [
  {
    data: {
      Product: "Product",
      Sites: "Sites",
      MSDSID: "MSDSID",
      MSDSLink: "MSDSLink",
      Business: "Business",
      HazMatNumber: "HazMatNumber"
    }
  },
  {
    data: {
      Product: "Product2",
      Sites: "Sites2",
      MSDSID: "MSDSID2",
      MSDSLink: "MSDSLink2",
      Business: "Business2",
      HazMatNumber: "HazMatNumber2"
    }
  }
]

console.log(myFunction());

function myFunction() {
    var arr = [];
    items.forEach(function(item) {
        var prod = item.data.Product;
        var sites= item.data.Sites;
        var mSDSID = item.data.MSDSID;
        var msdsLink = item.data.MSDSLink;
        var bus = item.data.Business;
        var hazMatNumber = item.data.HazMatNumber
        var str = '<tr><th>' + prod + '</th><th>' + sites+ '</th><th>' + bus + '</th><th>' + hazMatNumber + '</th><th><a href="' + msdsLink + '">' + mSDSID + '</a></th></tr>';
        arr.push(str);
    });
    return arr;
}

发布评论

评论列表(0)

  1. 暂无评论