I want to get the JSON from an array with this format
[
{
"title": "Name"
},
{
"title": "Phone"
},
{
"title": "Parent Phone"
},
{
"title": "Street"
}
]
I tried this code:
var favorite = new Array();
$.each($("#db_fields"), function() {
var field = {
'title': $(this).val()
};
favorite.push(field);
});
var myJsonString = JSON.stringify(favorite);
alert(myJsonString);
$("#db_fields") is a select (Bootstrap-select), it's an array of string
<select class="form-control" name="db_fields[]" id="db_fields" data-live-search="true" multiple >
<option value="Arabic Name"> Arabic Name</option>
<option value="Building"> Building</option>
</select>
but I got this result
[{"title":["Arabic Name","Phone","Building","Nationality"]}]
$("#db_fields")?$.each()with (only) an id should ring all your alarm bells as ids have to be unique.