Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
bagaimana cara looping multiple array/object menggunakan javascript?
ini adalah struktur json yang aku gunakan:
array:1 [â–¼
0 => array:3 [â–¼
"code" => "jne"
"name" => "Jalur Nugraha Ekakurir (JNE)"
"costs" => array:3 [â–¼
0 => array:3 [â–¼
"service" => "OKE"
"description" => "Ongkos Kirim Ekonomis"
"cost" => array:1 [â–¼
0 => array:3 [â–¼
"value" => 42000
"etd" => "4-5"
"note" => ""
]
]
]
1 => array:3 [â–¼
"service" => "REG"
"description" => "Layanan Reguler"
"cost" => array:1 [â–¼
0 => array:3 [â–¼
"value" => 48000
"etd" => "2-3"
"note" => ""
]
]
]
2 => array:3 [â–¼
"service" => "YES"
"description" => "Yakin Esok Sampai"
"cost" => array:1 [â–¼
0 => array:3 [â–¼
"value" => 104000
"etd" => "1-1"
"note" => ""
]
]
]
]
]
]
aku ingin menampilkan data service dan value menggunakan looping di jquery "each" bagaimana caranya?
//javascript
$('#kurir').change(function(){
var kurirID = $(this).val();
if(kurirID){
$.ajax({
type:"GET",
url:"{{url('checkout/kurir')}}?kurir_id="+kurirID,
success:function(res){
if(res){
$("#ongkir").prop('disabled', false);
$("#ongkir").empty();
$("#ongkir").append('<option>--- Pilih Kurir ---</option>');
$.each(res,function(key,value){
$("#ongkir").append('<option value="'+key['service']+'">'+res.value['service']+'</option>');
});
}else{
$("#ongkir").empty();
}
}
});
}else{
$("#ongkir").prop('disabled', true);
$("#ongkir").empty();
$("#ongkir").append('<option>--- Pilih Kurir ---</option>');
}
});
//controller
public function getKurirList(Request $request)
{
$data = RajaOngkir::Cost([
'origin' => 419, // id kota asal
'destination' => 114, // id kota tujuan
'weight' => 1700, // berat satuan gram
'courier' => 'jne', // kode kurir pengantar ( jne / tiki / pos )
])->get();
return response()->json($data);
}
ada yang bisa membantu aku??
0
1 Jawaban:
you can remove the outer loop and replace res with res.data:
$.each(res.data, function(k, v) {
/// do stuff
});
$.each(res, function() {
$.each(this, function(k, v) {
/// do stuff
});
});
source: https://stackoverflow.com/questions/733314/jquery-loop-over-json-result-from-ajax-success
0