mardi 16 janvier 2018

how to iterate array multidimesional with "each" javascript?

this in my code for example array from my json

$datas = array(
        array('code' => 'jne', 'name' => 'Jalur Nugraha Ekakurir (JNE)', 'costs' => 
            array(
            array('service' => 'OKE', 'description' => 'Ongkos Kirim Ekonomis', 'cost' => 
                array(
                array('value' => 42000, 'etd' => '4-5', 'note' => '')
                    )
                )


            ,

            array('service' => 'REG', 'description' => 'Layanan Reguler', 'cost' => 
                array(
                array('value' => 48000, 'etd' => '2-3', 'note' => '')
                    )
                ),

            array('service' => 'YES', 'description' => 'Yakin Esok Sampai', 'cost' => 
                array(
                array('value' => 104000, 'etd' => '1-1', 'note' => '')
                    )
                ))));

i try displayed data "service" and "value" with this code but is not working. i used javascript for dynamic select option:

$('#kurir').change(function(){
var kurirID = $(this).val();    
if(kurirID){
    $.ajax({
       type:"GET",
       url:"?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']+'">'+value['costs'][0]['service']+'</option>');
            });

        }else{

           $("#ongkir").empty();
        }
       }
    });
}else{
     $("#ongkir").prop('disabled', true);
    $("#ongkir").empty();
    $("#ongkir").append('<option>--- Pilih Kurir ---</option>');
} });

this my controller

public function getKurirList(Request $request) {
$data = RajaOngkir::Cost([ 'origin' => 419, 'destination' => 114, 'weight' => 1700, 'courier' => 'jne'])->get(); return response()->json($data); }

anyone help me??



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2EOOEb8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire