Javascript forEach在另一个函数里面

时间:2017-10-26 13:19:46

标签: javascript function foreach

INTRO: 我是JavaScript的新手,需要一些帮助。我想要做的是构建返回一个html字符串,我需要将一些变量引入到html表中。

问题: 我正在尝试forEach循环来收集我想要输出的数据。我可以循环并获取数据返回正常,但是我的forEach循环在另一个函数中。

代码:

'<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>'...

很抱歉,这有点难以阅读。

功能:

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;
    });
}

所以,既然我收到了数据,我怎样才能将每个str作为字符串返回给调用它的字符串?

4 个答案:

答案 0 :(得分:3)

您可以在.forEach之外声明变量,就像这样

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;
}

答案 1 :(得分:3)

我建议将reduce用于此目的,因为它的用途是什么。

library(plotrix)

pyramid.plot(xy.pop,xx.pop,labels=agelabels,
             main="Australian population pyramid 2002",lxcol=mcol,rxcol=fcol)

male.smline <- loess.smooth(x=1:18, y=xy.pop, degree=2)
lines(-1-male.smline$y, male.smline$x, col="red", lwd=3)

female.smline <- loess.smooth(x=1:18, y=xx.pop, degree=2)
lines(1+female.smline$y, female.smline$x, col="black", lwd=3)

答案 2 :(得分:3)

而不是forEach,您可以使用mapjoin生成的数组:

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("");
}

答案 3 :(得分:0)

ncardeli的回答已经很好了。但是,如果要将字符串作为数组获取,可以使用:

&#13;
&#13;
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;
}
&#13;
&#13;
&#13;