I have this array that was returned by json.
{1:Android, 2:IOS, 3:Business Management Systems, 4:Database, 5:Codes/Scripts, 6:Others}
or
1: "Android"
2: "IOS"
3: "Business Management Systems"
4: "Database"
5: "Codes/Scripts"
6: "Others"
This array is saved on a variable data
. And my current code of ajax
is:
$.post('fetchSubCategory',{val:val},function(data){
var subCategory = $.map(data, function(el){
return el;
});
$.each(subCategory, function(key, value) {
$('#subCategory')
.find('option')
.remove()
.end()
.append($('<option>', { value : key })
.text(value));
});
});
But it returns this [object Object]
. I want this array to populate my select
element that the values are those ids and the texts are those names. Something to be like this:
<select class="form-control square" id="subCategory" name="Category">
<option value="1">Android</option>
<option value="2">IOS</option>
<option value="3">Business Management Systems</option>
<option value="4">Database</option>
<option value="5">Codes/Scripts</option>
<option value="5">Others</option>
</select>
How could I achieve this on Javascript
or jquery
.