JSON 1
[{
"contactflag": "0",
"City": "America",
"District": "District I",
"zipcode": "4311"
}, {
"contactflag": "1",
"City": "America",
"District": "District II",
"zipcode": "4330"
}]
JSON 2
[{
"contactflag": "0",
"City": "Japan",
"District": "District I",
"zipcode": "7488"
}]
for (var ii = 0; ii < address.length; ii++) {
if (address[ii].contactflag == 1) {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
} else {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}
}
我有上面的示例JSON和我的代码。我想要做的是将地址推送到数组中。我的condition is to push contact address 1 if it exist otherwise push contact address 0
。但是,上面的代码会发生什么,即使有contact address 0
contact address 1
在示例1中:
当我只想获得["America, District I, 4311","America, District II, 4330"]
["America, District II, 4330"]
如何实现这一目标。
更新
答案 0 :(得分:4)
代码的问题在于,当值为<div class="container first-div">
<div class="main_head col-lg-6">
<h3>Here Are Just A Testing Content</h3>
<div class="take-right"> How to improve your skills. </div>
</div>
</div>
<div class="container second-div">
<div class="main_head col-sm-12">
<h3>Here Are Just A Testing Content</h3>
<h4> How to improve your skills. </h4>
</div>
</div>
<div class="container third-div">
<div class="main_head col-lg-5">
<span>Here Are Just A Testing Content</span>
<h4> How to improve your skills. </h4>
</div>
</div>
<div class="container first-div">
<div class="main_head col-lg-6">
<h3>Here Are Just A Testing Content</h3>
<h4> How to improve your skills. </h4>
</div>
</div>
<div class="container second-div">
<div class="main_head col-sm-12">
<h3>Here Are Just A Testing Content</h3>
<h4> How to improve your skills. </h4>
</div>
</div>
<div class="container third-div">
<div class="main_head col-lg-5">
<span>Here Are Just A Testing Content</span>
<h4> How to improve your skills. </h4>
</div>
</div>
?>
且1
时,代码会同时运行,因此它会推送两个值。
解决方案是删除其他部分
else
现在,它将在条件为真时推送值。
或只是 清空你的数组 :
for (var ii = 0; ii < address.length; ii++) {
if (address[ii].contactflag == 1) {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}
}
var address = [{
"contactflag": "1",
"City": "America",
"District": "District II",
"zipcode": "4330"
}, {
"contactflag": "0",
"City": "America",
"District": "District I",
"zipcode": "4311"
}],
addressTab = [];
for (var ii = 0; ii < address.length; ii++) {
if (address[ii].contactflag == 1) {
addressTab = []; // <-----make it empty here.
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
break; // <-----add this to break;
} else {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}
}
document.querySelector('pre').innerHTML = JSON.stringify(addressTab, 0, 4);
答案 1 :(得分:2)
下面应该适合你
for (var ii = 0; ii < address.length; ii++) {
if (address[ii].contactflag == "1") {
addressTab = [];
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}
else
{
addressTab.push(address[0].City + ", " + address[0].District + " " + address[0].zipcode);
}
}
答案 2 :(得分:2)
我想,这就是你想要做的事情:
var address = [{
"contactflag": "0",
"City": "Japan",
"District": "District I",
"zipcode": "7488"
}];
var addressTab = [];
for (var ii = 0; ii < address.length; ii++) {
if (address[ii].contactflag === "1") {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}
}
if (addressTab.length == 0) {
addressTab.push(address[0].City + ", " + address[0].District + " " + address[0].zipcode);
}
答案 3 :(得分:0)
address[ii].contactflag == 1
始终为true。使用address[ii].contactflag == "1"
,因为您已将1存储为字符串。
if (address[ii].contactflag == "1") {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
} else {
addressTab.push(address[ii].City + ", " + address[ii].District + " " + address[ii].zipcode);
}