javascript - 通过循环一个 javascript 对象来创建元素
问题描述
假设我们有一个对象数组,我们遍历它并在每次迭代中创建一个 div。
我们如何ol
在每次迭代中获取属性(这也是一个对象数组)并<ol>
在 div 内部为其中的每个对象创建一个,其中<li>
的 's 与属性中的数字一样多li
。此外,里面的li
文本应该是属性的文本,title
所以它应该是这样的:
我正在努力解决它,但我的菜鸟不允许我这样做。我在外循环内尝试了另一个ol
对象循环,但我无法正确处理。
var array_of_objects = [{
"name": "john",
"job": "pilot",
"email": "johnpilot@gmail.com",
"ol": [{
"li": 6,
"title": "6 li's"
}, {
"li": 2,
"title": "2 li's"
}, {
"li": 5,
"title": "5 li's"
}]
}, {
"name": "mark",
"job": "engineer",
"email": "markengineer@gmail.com",
"ol": [{
"li": 2,
"title": "2 li's"
}, {
"li": 7,
"title": "7 li's"
}, {
"li": 2,
"title": "2 li's"
}, {
"li": 1,
"title": "1 li's"
}]
}, {
"name": "george",
"job": "chef",
"email": "georgechef@gmail.com",
"ol": [{
"li": 1,
"title": "1 li's"
}, {
"li": 3,
"title": "3 li's"
}, {
"li": 4,
"title": "4 li's"
}, {
"li": 3,
"title": "3 li's"
}, {
"li": 3,
"title": "3 li's"
}]
}]
function iterate(arr) {
arr.forEach(i => {
var name = i.name;
var job = i.job;
var email = i.email;
var ol_li = i.ol_li;
var str = `<div>
<span>${name}</span>
<span>${job}</span>
<span>${email}</span>
<span>ol's should be placed here</span>
</div>`
$("body").append(str);
})
}
iterate(array_of_objects)
div {
display: flex;
flex-direction: column;
border: thin solid black;
margin: 0.4em;
padding: 0.2em;
}
span:not(:nth-child(4)) {
display: flex;
flex-direction: column;
border: thin solid red;
margin: 0.2em;
padding: 0.2em;
}
span:nth-child(4) {
display: flex;
flex-direction: row;
margin: 0.4em;
padding: 0.2em;
}
ol {
border: thin solid blue;
margin: 0.2em;
}
li {
margin: 0.2em;
}
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
解决方案
我认为这应该可以解决问题:
- 映射抛出
ol
数组并将单个li
项目放入变量中; - 使用
repeat()
函数在ol
变量中打印它
var array_of_objects = [
{
"name":"john",
"job":"pilot",
"email":"johnpilot@gmail.com",
"ol":[
{"li":6,"title":"6 li's"},
{"li":2,"title":"2 li's"},
{"li":5,"title":"5 li's"}
]
},
{
"name":"mark",
"job":"engineer",
"email":"markengineer@gmail.com",
"ol":[
{"li":2,"title":"2 li's"},
{"li":7,"title":"7 li's"},
{"li":2,"title":"2 li's"},
{"li":1,"title":"1 li's"}
]
},
{
"name":"george",
"job":"chef",
"email":"georgechef@gmail.com",
"ol":[
{"li":1,"title":"1 li's"},
{"li":3,"title":"3 li's"},
{"li":4,"title":"4 li's"},
{"li":3,"title":"3 li's"},
{"li":3,"title":"3 li's"}
]
}
]
function iterate(arr){
arr.forEach(i => {
var name = i.name;
var job = i.job;
var email = i.email;
var ol_li = i.ol;
var list = "";
var ol = ol_li.forEach(o=>{
l = `<li>${o.title}</li>`
list = `${list} <ol> ${l.repeat(o.li)} </ol>`
})
var str = `<div>
<span>${name}</span>
<span>${job}</span>
<span>${email}</span>
<span>${list}</ol>
</div>`
$("body").append(str);
})
}
iterate(array_of_objects)
div{
display:flex;
flex-direction:column;
border:thin solid black;
margin:0.4em;
padding:0.2em;
}
span:not(:nth-child(4)){
display:flex;
flex-direction:column;
border:thin solid red;
margin:0.2em;
padding:0.2em;
}
span:nth-child(4){
display:flex;
flex-direction:row;
margin:0.4em;
padding:0.2em;
}
ol{
border:thin solid blue;
margin:0.2em;
}
li{
margin:0.2em;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
</head>
<body>
</body>
</html>
推荐阅读
- database - 添加表时 User_tables 更新
- javascript - UIkit 模态警报 JS
- java - Jackson objectMapper 将不同的 json 属性映射到同一个 pojo
- java - 如何在字符 ASCII 字符串中以数字形式转换 java id?
- php - 使用 PHP MySQL 将数组放入 html 表
- json - 有没有一种优雅的方式来处理Scala中的Either Monad?
- javascript - 'Component' 未定义 no-undef 搜索关键字以了解有关每个错误的更多信息
- powershell - 获取 terraform 的输出 powershell 功能
- c - 如何将 UTF-8 字符串写入 Windows 控制台?
- amcharts - amchart v3 甘特图值轴固定间隔