1

I have this

var cartid = $(this).attr("data-cartid");

var cartid = 3;

        $.ajax({
          url:"/cart/update-quantity",
          method:'POST',
          data:{ cartid:cartid },
          dataType:'json',
          success:function(data)
          {

$('tr').filter('[data-cartid="'+cartid+'"]').find('.item_subtotal').text(data.3);

at the back it is being constructed and return as such

$ajaxdata[$cart->id] = $item_subtotal;
return json_encode($ajaxdata);

console.log output as 3: 480

If I call data.3, it is manually assign 3 now I want to auto map the cartid returned to here how can I dynamically call data.3

is there anything like eval that can let me call it dynamically as data.3?

i need help
  • 2,386
  • 10
  • 54
  • 72
  • Use bracket notation.... `data[3]` so `data[cartid]` – epascarello Nov 17 '20 at 16:17
  • wow, tried, this works! how about if I return to console.log as cart_3: 480? I tried this and it can't work $('tr').filter('[data-cartid="'+cartid+'"]').find('.item_subtotal').text(data.cart_[cartid]); – i need help Nov 17 '20 at 16:22

0 Answers0