skip to Main Content

I am successfully looping my data with ajax but when I console log I see my data sent in a new line.

$.ajax(settings).done(function(response) {
  $.get(settings).then(data => makeTable(data.contracts))
  const makeTable = (contracts) => {
      const body = document.getElementById('chart');
      contracts.forEach((person, index) => {
        person.details.forEach((entry) => {
          const arrayOfDigits = Array.from(String(entry.hours), Number);
          chart.updateSeries([{
            name: 'Hours',
            data: [entry.hours]
          }])

          console.log(arrayOfDigits)

I also tried converting my integers into an object but it would come out as

[integer]
[integer]
[integer]
[integer]
[integer]

But what I would like to do is having it in the same object like so

[integer, integer, integer, integer]

2

Answers


  1. Push arrayOfDigits into an array and log that at the end, rather than logging each one separately.

    const makeTable = (contracts) => {
      const body = document.getElementById('chart');
      const result = [];
      contracts.forEach((person, index) => {
        person.details.forEach((entry) => {
          const arrayOfDigits = Array.from(String(entry.hours), Number);
          chart.updateSeries([{
            name: 'Hours',
            data: [entry.hours]
          }])
          result.push(arrayOfDigits);
        });
      });
      console.log(result);
    }
    
    Login or Signup to reply.
  2. Final code working as intended:

    $.ajax(settings).done(function (response) {
        $.get(settings).then(data => makeTable(data.contracts))
        const makeTable = (contracts) => {
            const body = document.getElementById('chart');
            const result = [];
            contracts.forEach((person, index) => {
                person.details.forEach((entry) => {
                    result.push(entry.hours);
                });  
            }); 
        chart.updateSeries([{
                name: 'Hours',
                data: [result]
          }])
            console.log(result);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search