【发布时间】:2018-03-09 20:35:15
【问题描述】:
for (var of Object.values()) 只打印一个值,而不是全部。
不明白为什么会发生这种情况,因为它应该遍历每个值并打印出所有值。
现在只有第一个块显示值,而另一个不显示。我已将 cmets 放入 HTML,以便您可以看到应该在哪里打印来自 JSON 的结果。
const response = {
"internal": {
"services": {
"core": "OK",
"comments": "NOK",
"id": "OK"
},
"db": {
"pgsql": "OK",
"redis": "OK"
}
},
"external": {
"gitlab": "OK",
"trello": "OK",
"geonames": "OK"
}
}
const json = JSON.parse(response);
// Internal
for (let value of Object.values(json.internal.services)) {
if (value === 'OK') {
d3.select('#p-status').html('<p>No issues</p>');
d3.select('.status-icon').html('<i class="fas fa-check-circle"></i>');
d3.select('.status').classed('no-issues', true);
loading.remove;
} else {
d3.select('#p-status').html('<p>Has issues</p>');
d3.select('.status-icon').html('<i class="fas fa-exclamation-circle"></i>');
}
}
// External
for (let value of Object.values(json.external)) {
if (value === 'OK') {
d3.select('#p-status').html('<p>No issues</p>');
d3.select('.status-icon').html('<i class="fas fa-check-circle"></i>');
d3.select('.status').classed('no-issues', true);
loading.remove;
} else {
d3.select('#p-status').html('<p>Has issues</p>');
d3.select('.status-icon').html('<i class="fas fa-exclamation-circle"></i>');
}
}
body>div>div>div>div {
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
margin-bottom: 2.5%;
box-shadow: 0 0 10px #bfbfbf;
}
.fa-react {
font-size: 24px;
color: cyan;
}
.fa-comment {
font-size: 24px;
}
.p-name {
border-bottom: 1px solid #bfbfbf;
padding-bottom: 10px;
margin-bottom: 0;
}
#p-status {
font-weight: 700;
padding-top: 10px;
margin-top: 0;
}
#p-status p {
margin-top: 0;
}
.group {
padding: 0 10px;
}
.status-icon {
float: right;
}
.fa-check-circle {
font-size: 28px;
color: green;
padding-right: 5px;
}
.fa-exclamation-circle {
font-size: 28px;
color: red;
padding-right: 5px;
animation: pulseEff 1.5s infinite;
}
@keyframes pulseEff {
0% {
opacity: 1;
}
40% {
opacity: 1;
}
45% {
opacity: 0;
}
100% {
opacity: 0;
}
}
.has-issues {
border: 1px solid red;
}
.no-issues {
border: 1px solid green;
}
<div class="col-lg-6 col-md-12 col-xs-11">
<div class="status">
<div class="group">
<div class="row middle-xs">
<div class="col-lg-1 col-xs-2">
<i class="fab fa-react"></i>
</div>
<div class="col-lg-3 col-xs-4">
<p class="p-name">Core</p>
<div id="p-status">
<!-- this gets removed after the values are available, loading animation -->
<img src="./src/img/Double Ring-3s-200px.gif" width="30" height="30" alt="loading-animation">
</div>
</div>
<div class="col-lg-8 col-xs-6">
<div class="status-icon">
<!-- this gets removed after the values are available, loading animation -->
<img src="./src/img/Double Ring-3s-200px.gif" width="30" height="30" alt="loading-animation">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-12 col-xs-11">
<div class="status">
<div class="group">
<div class="row middle-xs">
<div class="col-lg-1 col-xs-2">
<i class="fas fa-comment"></i>
</div>
<div class="col-lg-3 col-xs-4">
<p class="p-name">Comments</p>
<div id="p-status">
<!-- this gets removed after the values are available, loading animation -->
<img src="./src/img/Double Ring-3s-200px.gif" width="30" height="30" alt="loading-animation">
</div>
</div>
<div class="col-lg-8 col-xs-6">
<div class="status-icon">
<!-- this gets removed after the values are available, loading animation -->
<img src="./src/img/Double Ring-3s-200px.gif" width="30" height="30" alt="loading-animation">
</div>
</div>
</div>
</div>
</div>
</div>
感谢任何帮助。我会回复每条评论和答案。
【问题讨论】:
-
大量无用的代码噪音。您可以将其本地化为几行代码。这样做时,您很有可能会自己发现问题。
-
一开始我想剪一些代码,但我想我应该把所有制作组件的代码都贴上去,相信我什至尝试了不同的方法,没有结果。
-
@JaredSmith 试过,打印出
OK。
标签: javascript for-loop iteration