gpt4 book ai didi

javascript - 如何正确使用 map() 来迭代数组元素?

转载 作者:行者123 更新时间:2023-12-03 08:22:32 26 4
gpt4 key购买 nike

originally写了一个关于 document.write() 的问题。然而,像往常一样,每个人都说这是愚蠢的。因此,我改变了方法并使用建议的 getElementsByClassName() 并更改了数据的排列方式。这些东西都是我的……现在,当我迭代我的东西时,它会输出每个标题,而不是仅仅一个。

我该如何修复this(codepen)为每个日期或地点创建一个新的 div,而不是将所有地点/日期放入一个 div 中?

<html>
<body>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>


<style>
.note {
background-color: khaki;
}
.note-message {
background-color: peachpuff;
}
[class] {
padding: 1em;
}

.cat{
color:red;
}
</style>
</head>

<div class="container">

<article>
<p class="class-name">
LOCATION NUMBER ONE has a class of "class-name"
</p>

<p>
LOCATION NUMBER TWO has no class name.
</p>

<p class="class-name">
LOCATION NUMBER THREE has a class of "class-name"
</p>

<p>
LOCATION NUMBER FOUR has no class name.
</p>

<p class="class-name">
LOCATION NUMBER FIVE has a class of "class-name"
</p>

<p>
LOCATION NUMBER SIX has a no class name.
</p>

<p class="class-name">
LOCATION NUMBER SEVEN has a class of "class-name"
</p>

<p>
LOCATION NUMBER EIGHT has no class name.
</p>

<p class="class-name">
LOCATION NUMBER NINE has a class of "class-name"
</p>

</article>
</div> <!-- CONTAINER -->
</body>
</html>
<script>

var events = [
{place: "Orlando", date: "New York", eventName: "Cats & Dogs"},
{place: "New York", date: "November 5-8, 2015", eventName: "Meet and Greet"},
{place: "Detroit", date: "November 5-8, 2015", eventName: "Meet and Greet"},
{place: "Boston", date: "April 5-8, 2015", eventName: "Meet and Greet"},
{place: "Boston", date: "November 5-8, 2015", eventName: "Drink Coffee"},
{place: "Boston", date: "July 5-8, 2015", eventName: "Drink Coffee"},
{place: "Phoenix", date: "December 5-8, 2015", eventName: "Drink Coffee"},
{place: "Phoenix", date: "July 5-8, 2015", eventName: "Dance With Me"},
{place: "Phoenix", date: "April 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "December 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "December 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "October 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "September 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "May 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "August 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "February 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "June 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "May 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "February 5-8, 2015", eventName: "I Married an Axe Murderer"},
{place: "Boston", date: "September 5-8, 2015", eventName: "Meet The Authors"},
{place: "San Antonio", date: "August 5-8, 2015", eventName: "Meet The Authors"},
{place: "San Antonio", date: "January 5-8, 2015", eventName: "Sponsorship"},
{place: "San Antonio", date: "October 5-8, 2015", eventName: "Sponsorship", },
{place: "Boston", date: "January 5-8, 2015", eventName: "Soylent Green"},
{place: "Boston", date: "October 5-8, 2015", eventName: "Dance With The Devil"},
{place: "Boston", date: "August 5-8, 2015", eventName: "Getting Started"}
];


function insertTags(before){
"use strict";

var els = document.getElementsByClassName("class-name");

Array.prototype.forEach.call(els, function(el) {

var breakTag = window.document.createElement("br" );
var note = window.document.createElement("h3" );
var noteP = window.document.createElement("p" );

breakTag.setAttribute("class","clear");
note.setAttribute("class","note");
noteP.setAttribute("class","note-message");

note.appendChild(noteP);


noteP.innerHTML = events.map(function(entry) {
return entry.eventName;
});

el.parentNode.insertBefore( breakTag, el );
el.parentNode.insertBefore( note, el );

return breakTag;
return note;
});
}
insertTags();
</script>

最佳答案

尝试替换 div p 的元素元素位于 noteP ,返回p来自 .map() 的字符串元素,使用 Array.prototype.join() "<br>"范围 ;删除return breakTag , return note

function insertTags(before){
"use strict";

var els = document.getElementsByClassName("class-name");

Array.prototype.forEach.call(els, function(el) {

var breakTag = window.document.createElement("br" );
var note = window.document.createElement("h3" );
// create `div` element
var noteDiv = window.document.createElement("div" );

breakTag.setAttribute("class","clear");
note.setAttribute("class","note");
noteDiv.setAttribute("class","note-message");

note.appendChild(noteDiv);

noteDiv.innerHTML = events.map(function(entry) {
// return `p` element
return "<p>" + entry.eventName + "</p>";
}).join("<br>"); // join array items with `<br>`

el.parentNode.insertBefore( breakTag, el );
el.parentNode.insertBefore( note, el );

// return breakTag;
// return note;
});
}
insertTags();

<html>
<body>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>


<style>
.note {
background-color: khaki;
}
.note-message {
background-color: peachpuff;
}
[class] {
padding: 1em;
}

.cat{
color:red;
}
</style>
</head>

<div class="container">

<article>
<p class="class-name">
LOCATION NUMBER ONE has a class of "class-name"
</p>

<p>
LOCATION NUMBER TWO has no class name.
</p>

<p class="class-name">
LOCATION NUMBER THREE has a class of "class-name"
</p>

<p>
LOCATION NUMBER FOUR has no class name.
</p>

<p class="class-name">
LOCATION NUMBER FIVE has a class of "class-name"
</p>

<p>
LOCATION NUMBER SIX has a no class name.
</p>

<p class="class-name">
LOCATION NUMBER SEVEN has a class of "class-name"
</p>

<p>
LOCATION NUMBER EIGHT has no class name.
</p>

<p class="class-name">
LOCATION NUMBER NINE has a class of "class-name"
</p>

</article>
</div> <!-- CONTAINER -->
<script>

var events = [
{place: "Orlando", date: "New York", eventName: "Cats & Dogs"},
{place: "New York", date: "November 5-8, 2015", eventName: "Meet and Greet"},
{place: "Detroit", date: "November 5-8, 2015", eventName: "Meet and Greet"},
{place: "Boston", date: "April 5-8, 2015", eventName: "Meet and Greet"},
{place: "Boston", date: "November 5-8, 2015", eventName: "Drink Coffee"},
{place: "Boston", date: "July 5-8, 2015", eventName: "Drink Coffee"},
{place: "Phoenix", date: "December 5-8, 2015", eventName: "Drink Coffee"},
{place: "Phoenix", date: "July 5-8, 2015", eventName: "Dance With Me"},
{place: "Phoenix", date: "April 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "December 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "December 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "October 5-8, 2015", eventName: "Dance With Me"},
{place: "Boston", date: "September 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "May 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "August 5-8, 2015", eventName: "Kiss Me... I'm Irish"},
{place: "Orlando", date: "February 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "June 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "May 5-8, 2015", eventName: "Potty Training"},
{place: "Boston", date: "February 5-8, 2015", eventName: "I Married an Axe Murderer"},
{place: "Boston", date: "September 5-8, 2015", eventName: "Meet The Authors"},
{place: "San Antonio", date: "August 5-8, 2015", eventName: "Meet The Authors"},
{place: "San Antonio", date: "January 5-8, 2015", eventName: "Sponsorship"},
{place: "San Antonio", date: "October 5-8, 2015", eventName: "Sponsorship", },
{place: "Boston", date: "January 5-8, 2015", eventName: "Soylent Green"},
{place: "Boston", date: "October 5-8, 2015", eventName: "Dance With The Devil"},
{place: "Boston", date: "August 5-8, 2015", eventName: "Getting Started"}
];


function insertTags(before){
"use strict";

var els = document.getElementsByClassName("class-name");

Array.prototype.forEach.call(els, function(el) {

var breakTag = window.document.createElement("br" );
var note = window.document.createElement("h3" );
var noteDiv = window.document.createElement("div" );

breakTag.setAttribute("class","clear");
note.setAttribute("class","note");
noteDiv.setAttribute("class","note-message");

note.appendChild(noteDiv);


noteDiv.innerHTML = events.map(function(entry) {
return "<p>" + entry.eventName + "</p>";
}).join("<br>");

el.parentNode.insertBefore( breakTag, el );
el.parentNode.insertBefore( note, el );

});
}
insertTags();
</script>
</body>
</html>

关于javascript - 如何正确使用 map() 来迭代数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33685663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com