如何将检查值和变量值推送到json数组中

时间:2017-10-13 06:11:19

标签: javascript jquery json

这里我有一个表单,一个表单字段是复选框,假设我点击 Ward robe&点亮并点击提交按钮意味着我正在使用此值并推送到我的json格式到现在它工作正常,我的问题是我必须变量假设在那个变量!空意味着我想把这个值推入我的json.one更多的条件我们也要检查像假设在形式我点击值是Ward robe&冰箱和我的 var needtopushval ='冰箱'; Fride是两次,所以不应该把这个变量推到我的json中

regex   = '<(script|img|link).*(src|href)=[\'\"][/]users/(id/|)[\d]+/.*[\'\"]'
pattern = re.compile(regex, re.IGNORECASE)
link    = pattern.search(html_source_code).group(0)

user_pattern = re.compile('[\d.]+', re.IGNORECASE)
user         = user_pattern.search(link).gourp(0)
 function rentfunction(){
 var needtopushval=' Lights';
    var arr1 = [];
    var data={
            "rentProperty": {
                            "fullName" : "Some Name"
                            },
            "floorType":[]
        };
                $.each($("input[name='furniture_check']:checked"),function(){
                var furniture = $(this).val();
                arr1.push({"floorTypeName": furniture }); 
                });
    arr1.push({"floorTypeName": needtopushval });

             data.floorType=arr1;
            //or data["floorType"]=arr1;

                console.log(data);
          }

  

我的预期JSON

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form>
	<input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br>
	<input type="checkbox" name="furniture_check" value="Lights">Lights <br>
	<input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br>
  
  <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button>
</form>

2 个答案:

答案 0 :(得分:0)

我希望它可以帮助您解决问题

&#13;
&#13;
 function rentfunction(){
     var needtopushval='Lights';
     var arr1 = [];
     var data={
            "rentProperty": {
                            "fullName" : "Some Name"
                            },
            "floorType":[]
        };
    // 
     $.each($("input[name='furniture_check']:checked"),function(e)      {
       var furniture = $.trim($(this).val());
       var index=data.floorType.indexOf({"floorTypeName": furniture });
         if(index==-1){
              data.floorType.push({"floorTypeName": furniture });
           }
      });
    var flag=0;
    $.each(data.floorType,function(e){             
         if($.trim(data.floorType[e].floorTypeName) === $.trim(needtopushval)){
           flag=1;
         }
    });
     if(flag==0){
         data.floorType.push({"floorTypeName": $.trim(needtopushval)});
     }
    
    console.log(data);
}
&#13;
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <form>
    	<input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br>
    	<input type="checkbox" name="furniture_check" value="Lights">Lights <br>
    	<input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br>
      
      <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button>
    </form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

希望以下代码有帮助

用于检查非空值并且已经存在该值的代码

var pos = arr1.map(function(e) {
    return e.floorTypeName;
}).indexOf(furniture);

if (pos == -1 && needtopushval) {
    arr1.push({
        "floorTypeName": needtopushval
    });
}

function rentfunction() {
    var needtopushval = ' Lights';
    var arr1 = [];
    var data = {
        "rentProperty": {
            "fullName": "Some Name"
        },
        "floorType": []
    };
    $.each($("input[name='furniture_check']:checked"), function() {
        var furniture = $(this).val();
        if (furniture) {
            arr1.push({
                "floorTypeName": furniture
            });
        }

        //Code to Check weather a value exist and notempty
        var pos = arr1.map(function(e) {
            return e.floorTypeName;
        }).indexOf(furniture);


        if (pos == -1 && needtopushval) {
            arr1.push({
                "floorTypeName": needtopushval
            });
        }

    });

    data.floorType = arr1;
    //or data["floorType"]=arr1;

    console.log(data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form>
	<input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br>
	<input type="checkbox" name="furniture_check" value="Lights">Lights <br>
	<input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br>
  
  <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button>
</form>