Reputation: 33
Here is my trying code, I am trying to generate string to multiple URLs
var customfield_13731 = "one,two,three,four,one,two,three,four"
let chunks = customfield_13731.split(",");
let res = [];
let output = [];
while (chunks.length) {
let [id, name, key, test] = chunks.splice(0, 4);
output.push({ id, name, key, test });
}
output.map((item) => {
let url = `https://test.io/639422/${item.id}/${item.name}/${item.key}/${item.test}`;
console.log(url)
document.getElementById("URL").innerHTML = url;
document.getElementById("URL").setAttribute("href", url);
});
when I am getting console.log(URL), I get multiple URL links, but I need to display a browser by dom. I can not display multiple URL in UI
My expected output would be :
https://test.io/639422/one/two/three/four
https://test.io/639422/one/two/three/four
Upvotes: 2
Views: 67
Reputation: 6757
You need to append it by using +=
and not =
in your innerHTML
assignment.
Also:
forEach
not map
if you don't change the arraya
tagvar customfield_13731 = "one,two,three,four,five,six,seven,eight"
let chunks = customfield_13731.split(",");
let res = [];
let output = [];
while (chunks.length) {
let [id, name, key, test] = chunks.splice(0, 4);
output.push({ id, name, key, test });
}
output.forEach((item) => {
let url = `https://test.io/639422/${item.id}/${item.name}/${item.key}/${item.test}`;
document.getElementById("url").innerHTML
+= `<a href="${url}">${url}</a><br>`; // append not replace!
});
<div id="url"></div>
Edit (Thank you @Thomas)
A better way would be to concat the strings before adding them to the DOM, opposed to adding them one after the other. This would be a way to do this:
var customfield_13731 = "one,two,three,four,five,six,seven,eight"
let chunks = customfield_13731.split(",");
let res = [];
let output = [];
while (chunks.length) {
let [id, name, key, test] = chunks.splice(0, 4);
output.push({ id, name, key, test });
}
const urlParts = output.map((item) => {
let url = `https://test.io/639422/${item.id}/${item.name}/${item.key}/${item.test}`;
return `<a href="${url}">${url}</a><br>`;
}).join('');
document.getElementById("url").innerHTML = urlParts;
<div id="url"></div>
Upvotes: 1
Reputation: 12637
Yet another version.
I have added a utility function, because it's not the first time I had to turn a long list into rows/pages/groups, and DRY.
// DRY, not the first time I needed this.
const toGroupsOf = (n, array) => Array.from({ length: Math.ceil(array.length/n)|0 }, (_,i) => array.slice(n*i, n*i+n));
var customfield_13731 = "one,two,three,four,five,six,seven,eight";
document.getElementById("URL").innerHTML = toGroupsOf(4, customfield_13731.split(","))
.map(([id,name,key,test]) => {
let url = `https://test.io/639422/${id}/${name}/${key}/${test}`;
return `<a href="${url}">${url}</a>`;
})
.join("\n");
a { display: block }
<div id="URL"></div>
Upvotes: 1
Reputation: 20039
You can use createElement()
to create multiple anchor elements
var customfield_13731 = "one,two,three,four,one,two,three,four"
let chunks = customfield_13731.split(",");
let res = [];
let output = [];
while (chunks.length) {
let [id, name, key, test] = chunks.splice(0, 4);
output.push({ id, name, key, test });
}
output.map((item) => {
let url = `https://test.io/639422/${item.id}/${item.name}/${item.key}/${item.test}`;
console.log(url)
let a = document.createElement('a');
a.innerHTML = url;
a.setAttribute("href", url);
document.getElementById("URL").appendChild(a);
});
a { display: block }
<div id="URL"></div>
Upvotes: 1