Reputation: 65
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?
Upvotes: 2
Views: 4418
Reputation: 2238
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;
});
}
Upvotes: 3
Reputation: 5891
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;
}
Upvotes: 0
Reputation: 3492
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("");
}
Upvotes: 3
Reputation: 6739
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;
}
Upvotes: 3