I got the following HTML:
<div class="slot_subclass">
<div class="form-group">
<input type="text" class="form-control slot" name="slot_name">
</div>
<div class="form-group">
<input type="text" class="form-control slot" name="slot_type">
</div>
</div>
<div class="slot_subclass">
<div class="form-group">
<input type="text" class="form-control slot" name="slot_name">
</div>
<div class="form-group">
<input type="text" class="form-control slot" name="slot_type">
</div>
</div>
<div class="slot_subclass">
<div class="form-group">
<input type="text" class="form-control slot" name="slot_name">
</div>
<div class="form-group">
<input type="text" class="form-control slot" name="slot_type">
</div>
</div>
The amount of slot_subclass may be unlimited.
I need to parse all these inputs and make an object where the key is the value of first input of slot_subclass (slot_name) and value is the value of the second (slot_type).
I tried the following:
$(".slot").map(function(){
return $(this).val();
}).get();
but I just get a plain array of values. I may use jQuery for this task. Thank you.
UPD_1 In order to handle for the same keys I chose the following code (if someone interested):
jsonObj = [];
$(".slot_subclass").each(function() {
var slot_name = $(this).find("input[name=slot_name]").val();
var slot_type = $(this).find("input[name=slot_type]").val();
item = {};
item[slot_name] = slot_type;
jsonObj.push(item);
});
console.log(jsonObj);
Thank you everyone for help.
map()returns an array. In your example this array will contain values only, not key:value pairs. The issue you have is that you have multiple fields with the same name, which would not be valid in a single object.nameis tha value of the first input inslot_subclassandvalueis the value of second input. Ideally no class names or htmlnameattributes would be present