0

I had a little problem with retrieving data from object jQuery. I made some data to object, and some data is from jquery ajax.

If Log the data, the data was there, but When I try to get the data the log said undefined, but when I try to get the another data the log thrown the data. For example :

console.log(v);
console.log(v.totalOngkir);

will output like this : the output

the output of v, if it's expand : the output

v said it has totalOngkir, but when I want to take it from v.totalOngkir it said undefined. but when I tried to take another value from v, its normally shown.

P.S. I am using $.when for wait the result of ajax,

Here's my code :

$(document).ready(function(){
  var listCart = {};

  function formatDigit(str){
    return str.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.");
  }

  function cekOngkir(weight,origin,destination,kurir,servis) {
    return $.ajax({
      url:"/courier/cekongkir",
      type:"POST",
      data:"weight="+weight+"&origin="+origin+"&destination="+destination+"&kurir="+kurir+"&servis="+servis,
      success:function(e){}
    });
  }

  function listEachCart(i,e){

    var totalWeight = 0;
    var totalPrice = 0;

    $(e).find(".theProduct").map(function(i1,e1){
      totalWeight += parseFloat($(e1).attr("totalBerat"));
      totalPrice += parseInt($(e1).attr("totalHarga"));
    });

    var tempArray = {};
    tempArray['totalWeight'] = totalWeight;
    tempArray['totalPrice'] = totalPrice;

    $.when(cekOngkir(tempArray.totalWeight,listCart[i].kecamatanToko,listCart[i].kecamatanUser,listCart[i].kodeKurir,listCart[i].kodeServis)).done(function(es){
      tempArray['totalOngkir'] = parseInt(es);
      // console.log(es);
    });

    listCart[i] = Object.assign(tempArray,listCart[i]);
    // console.log(listCart[i]);
  }

  var deferredArr =
  $(".listCart").map(function(i,e){
    var listGroup = {};

    listGroup['idToko'] = $(e).attr("idToko");
    listGroup['idUsersAlamat'] = $(e).attr("idUsersAlamat");
    listGroup['kecamatanToko'] = $(e).attr("kecamatanToko");
    listGroup['kecamatanUser'] = $(e).attr("kecamatanUser");
    listGroup['idKurir'] = $(e).attr("idKurir");
    listGroup['kodeKurir'] = $(e).attr("kodeKurir");
    listGroup['kodeServis'] = $(e).attr("kodeServis");
    listGroup['element'] = e;

    listCart[i] = listGroup;

    listEachCart(i,e);

  });

  $.when.apply(this,deferredArr).then(function(){
    $.map(listCart,function(v,i){
      console.log(Object.keys(v));
      console.log(v.totalOngkir);
      // $(v.element).find(".totalOngkir").html("Rp. "+formatDigit((v.totalOngkir).toString()));
    });
  });
});

0 Answers0