How to create json by JavaScript for loop?

后端 未结 5 889
滥情空心
滥情空心 2020-11-30 17:18

I have array of select tag.


                        
    
提交评论

  • 2020-11-30 17:26

    From what I understand of your request, this should work:

    <script>
    //  var status  = document.getElementsByID("uniqueID"); // this works too
    var status  = document.getElementsByName("status")[0];
    var jsonArr = [];
    
    for (var i = 0; i < status.options.length; i++) {
        jsonArr.push({
            id: status.options[i].text,
            optionValue: status.options[i].value
        });
    }
    </script>
    
    0 讨论(0)
  • 2020-11-30 17:42

    If I want to create JavaScript Object from string generated by for loop then I would JSON to Object approach. I would generate JSON string by iterating for loop and then use any popular JavaScript Framework to evaluate JSON to Object.

    I have used Prototype JavaScript Framework. I have two array with keys and values. I iterate through for loop and generate valid JSON string. I use evalJSON() function to convert JSON string to JavaScript object.

    Here is example code. Tryout on your FireBug Console

    var key = ["color", "size", "fabric"];
    var value = ["Black", "XL", "Cotton"];
    
    var json = "{ ";
    for(var i = 0; i < key.length; i++) {
        (i + 1) == key.length ? json += "\"" + key[i] + "\" : \"" + value[i] + "\"" : json += "\"" + key[i] + "\" : \"" + value[i] + "\",";
    }
    json += " }";
    var obj = json.evalJSON(true);
    console.log(obj);
    
    0 讨论(0)
  • 2020-11-30 17:46
    var sels = //Here is your array of SELECTs
    var json = { };
    
    for(var i = 0, l = sels.length; i < l; i++) {
      json[sels[i].id] = sels[i].value;
    }
    
    0 讨论(0)
  • 2020-11-30 17:46

    If you want a single JavaScript object such as the following:

    { uniqueIDofSelect: "uniqueID", optionValue: "2" }
    

    (where option 2, "Absent", is the current selection) then the following code should produce it:

      var jsObj = null;
      var status = document.getElementsByName("status")[0];
      for (i = 0, i < status.options.length, ++i) {
         if (options[i].selected ) {
            jsObj = { uniqueIDofSelect: status.id, optionValue: options[i].value };
            break;
         }
      }
    

    If you want an array of all such objects (not just the selected one), use michael's code but swap out status.options[i].text for status.id.

    If you want a string that contains a JSON representation of the selected object, use this instead:

      var jsonStr = "";
      var status = document.getElementsByName("status")[0];
      for (i = 0, i < status.options.length, ++i) {
         if (options[i].selected ) {
            jsonStr = '{ '
                      + '"uniqueIDofSelect" : '
                      + '"' + status.id + '"'
                      + ", "
                      + '"optionValue" : '
                      + '"'+ options[i].value + '"'
                      + ' }';
            break;
         }
      }
    
    0 讨论(0)
  • 提交回复
    热议问题