[Answered ]-Pass associative array through Ajax

2πŸ‘

βœ…

  1. You are not using $.each() correctly. Try this:

    $.each($(β€˜[id$=”-subtotal”]’), function (key, element) {
    subtotal = some_math;
    id = some_ID_Number;
    data[β€˜id-β€˜ + String(id)] = subtotal
    });


  1. You should declare data as an object, not an array. An array can only have numeric keys. An object can have keys that contain other characters. If you console.log(data) you will see that data will contain data if it’s an object, and will be empty if initialized as an array. So try this:
  var data = {};

  $.each($('*'), function() {
    subtotal = Math.random() * 100;
    id = Math.random() * 100;
    data['id-' + String(id)] = subtotal
  });

  console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
πŸ‘€Koen

Leave a comment