选择值来推送jquery

时间:2016-02-24 08:29:35

标签: jquery json

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"]

如何实现这一目标。

更新

jsfiddle

4 个答案:

答案 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);
}