Loop in Javascript to display DB Values

I am a python developer and new to javascript and ajax . I am creating a pie chart that displays DB values.

$(document).ready(function () {
      google.charts.load('current', { 'packages': ['corechart'] });
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        $.ajax({
            url: '/dashboard/document/',
            type: 'POST',
            dataType: 'json',
            data:{
              csrfmiddlewaretoken: '{{ csrf_token }}'
            },
            success: function (response) {
                console.log(response.result);
                $.each(response.result, function (item) {
                  console.log(item);

I have fetched my DB values using pymongo and it is stored in console.log(response.result);Now i need to print in console.log(item); using looping . How to use ?

1 answer

  • answered 2020-06-27 04:20 sheriffderek

    i need to print in console.log(item); using looping . How to use ?

    You can probably skip the jQuery if you are new to JS. (the language has come a long way and doesn't really need it often anymore : )

    const exampleArray = ['one', 'two', 'three'];
    
    exampleArray.forEach( function(thing) {
        console.log('thing: ', thing);
    });
    

    https://jsfiddle.net/sheriffderek/h2fcb54g/