Want to fetch data from input and construct in JSON

53
December 27, 2018, at 10:30 AM

I am trying to fetch the data which is stored in a table in a JSON format, but while it is using the .each to fetch data, then the latest value is getting repeated all the time.

I have shown the actual output where it should have different data from the table rows

function getPackTableDetails() {
                var packDetailsRow = [];
                var tableRowCount = $('#addPackTable tr').length;
                var returnJSONFormat = "";
                    $('#addPackTable tr').each(function(index) {
                        if(index != 0) {
                            var packDetailsObj = {};
                            ($(this).find("td")).each(function(idx) {
                                switch(idx) {
                                    case 0:
                                        packDetailsObj.table_pack_name = $('#table_pack_name').val();
                                    break;
                                    case 1:
                                        packDetailsObj.table_pack_type = $('#table_pack_type').val();
                                    break;
                                    case 2:
                                        packDetailsObj.base_amount = $('#base_amount').val();
                                    break;
                                    case 3:
                                        packDetailsObj.tax_amount = $('#tax_amount').val();
                                    break;
                                    case 4:
                                        packDetailsObj.total_amount = $('#total_amount').val();
                                    break;
                                }
                            });
                            packDetailsRow.push(packDetailsObj);
                        }
                    });
                    // console.log(JSON.stringify(packDetailsRow));
                    return packDetailsRow;
            }

As i am using a input type text where i use value to store my data and then make it retrieve to the following jquery code

Output:
{
  "pack_details": [
    {
      "table_pack_name": "Demo Pack Name5",
      "table_pack_type": "Demo Pack Name5",
      "base_amount": "200.00",
      "tax_amount": "0.00",
      "total_amount": "200.00"
    },
    {
      "table_pack_name": "Demo Pack Name5",
      "table_pack_type": "Demo Pack Name5",
      "base_amount": "200.00",
      "tax_amount": "0.00",
      "total_amount": "200.00"
    },
    {
      "table_pack_name": "Demo Pack Name5",
      "table_pack_type": "Demo Pack Name5",
      "base_amount": "200.00",
      "tax_amount": "0.00",
      "total_amount": "200.00"
    }
  ]
}

Actual Output:

{
  "pack_details": [
    {
      "table_pack_name": "Demo Pack Name5",
      "table_pack_type": "Demo Pack Name5",
      "base_amount": "20.00",
      "tax_amount": "10.00",
      "total_amount": "30.00"
    },
    {
      "table_pack_name": "Demo Pack Name6",
      "table_pack_type": "Demo Pack Name6",
      "base_amount": "200.00",
      "tax_amount": "0.00",
      "total_amount": "200.00"
    },
    {
      "table_pack_name": "Demo Pack Name7",
      "table_pack_type": "Demo Pack Name7",
      "base_amount": "400.00",
      "tax_amount": "40.00",
      "total_amount": "440.00"
    }
  ]
}
Answer 1

because you're assigning the same values every iteration

switch(idx) {
  case 0:
    packDetailsObj.table_pack_name = $('#table_pack_name').val();
    break;
  case 1:
    packDetailsObj.table_pack_type = $('#table_pack_type').val();
    break;
  case 2:
    packDetailsObj.base_amount = $('#base_amount').val();
    break;
  case 3:
    packDetailsObj.tax_amount = $('#tax_amount').val();
    break;
  case 4:
    packDetailsObj.total_amount = $('#total_amount').val();
    break;
}
Answer 2

you need to iterate through td in ($(this).find("td")).each

$('#addPackTable tr').each(function (index) {
  if (index != 0) {
    var packDetailsObj = {};
    ($(this).find("td")).each(function (idx) {
      switch (idx) {
        case 0:
          packDetailsObj.table_pack_name = $('td #table_pack_name').val();
          break;
        case 1:
          packDetailsObj.table_pack_type = $('td #table_pack_type').val();
          break;
        case 2:
          packDetailsObj.base_amount = $('td #base_amount').val();
          break;
        case 3:
          packDetailsObj.tax_amount = $('td #tax_amount').val();
          break;
        case 4:
          packDetailsObj.total_amount = $('td #total_amount').val();
          break;
      }
    });
    packDetailsRow.push(packDetailsObj);
  }
});
READ ALSO
Error while running fastStructure: ImportError: No module named allelefreq

Error while running fastStructure: ImportError: No module named allelefreq

I'm trying to run faststructure softwareAftering installing numpy, scipy, cython, gsl1

73
Application killed by Android system

Application killed by Android system

In my app, I have Activity A ( home activity) --->> then Activity B --->> Then foreground serviceI discovered that after some time, I got in notification that this app is consuming power > 40mhA

61
How do I disable the Facebook login button from automatically switching to “Log out”?

How do I disable the Facebook login button from automatically switching to “Log out”?

I have correctly followed the documentation for Facebook Login on Android appThe login button works fine, except, I don't like how it automatically switches states

32
difference between data access layer and database layer [on hold]

difference between data access layer and database layer [on hold]

I have read somewhere that data access layer is same as database layer

51