[英]JavaScript Array.map() appends commas to the DOM
I'm attempting to render a few lottery results from here . 我试图从这里渲染一些彩票结果。 I am getting all the data just fine, but want to combine the winning_numbers, mega_ball, and multiplier values to render to the page like this:
我得到的所有数据都很好,但是想要合并winning_numbers,mega_ball和乘数值以呈现到页面上,如下所示:
Instead what I am getting is the numbers but with commas rendered to the DOM as well like this shows: 取而代之的是,我得到的是数字,但对DOM呈现了逗号,如下所示:
const url = 'https://data.ny.gov/resource/h6w8-42p9.json';
$.get(url, (data) => {
// Deconstruct Array Data
[latest, one, two, three] = data;
const pastResults = [one, two, three]
// Removed A Block of CODE for StackOverflow dealing with latest result since it works fine.
pastResults.map(num => {
const numsArray = num.winning_numbers.split(" ");
numsArray.push(num.mega_ball);
numsArray.push("x" + num.multiplier.split("")[1]);
console.log(numsArray);
pastResultsBlock.append(`
<div class="col-12">
<div class="animated flipInY past-result-item box-shadow">
<div class="past-result-date">${moment(num.draw_date).format("MMM Do")}</div>
<div class="past-result-numbers">
${numsArray.map(number => `<span>${number}</span>`)}
</div>
</div>
</div>
`)
})
})
In the console the array looks just fine. 在控制台中,阵列看起来还不错。 but when it renders in the DOM there is a "," comma after every ...
但是,当它在DOM中呈现时,每...后都有一个“,”逗号。
Any help and feedback would be greatly appreciated. 任何帮助和反馈将不胜感激。
Here is a JSFiddle link just in case. 这是一个JSFiddle链接,以防万一。 JSFiddle
的jsfiddle
Simply join them explicitly instead of relying on the default Array::toString
method. 只需显式地加入它们,而不是依赖默认的
Array::toString
方法。
// Change
`...${numsArray.map(number => `<span>${number}</span>`)}...`
// to
`...${numsArray.map(number => `<span>${number}</span>`).join("")}...`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.