Search code examples
javascriptarraysjavascript-objects

Javascript for loop within function not pushing to array


EDIT - code to calculate refill_playlist_len included

I have a function in Javascript that deletes a row of an HTML table and populates it again with values from arrays.

Within this deleteRow function, I have a for loop which loops through a string and assigns parts of the strings to different variables and tries to push them onto arrays.

Without the for loop, it works fine (i.e. when I just index manually) but for some reason when I place it in a for loop, the values aren't pushed onto the arrays. The values themselves print fine on each iteration they just aren't added to the array.

Refill_playlist_len is the count of the Django Queryset (30).


var refill_playlist_len = '{{ playlist.count }}';

var artist_Arr = [];
var track_Arr = [];
var track_id_Arr = [];
var album_Arr = [];
var artist_name;
var track_name;
var track_id;
var album_name;

for (var i = 0; i < refill_playlist_len; i++) {
      var searchStr = refill_playlist[i];
      console.log(searchStr);
      console.log(typeof searchStr);
      console.log(typeof refill_playlist);

      //grab variables
      artist_name = searchStr.match(new RegExp("artist_name:" + "(.*)" + ", album_name:"));
      console.log(artist_name[1]);
      artist_Arr.push(artist_name[1]);

      track_name = searchStr.match(new RegExp("track_name:" + "(.*)" + ", acousticness:"));
      console.log(track_name[1]);
      track_Arr.push(track_name[1]);

      track_id = searchStr.match(new RegExp("track_id:" + "(.*)" + ", track_name:"));
      console.log(track_id[1]);
      track_id_Arr.push(track_id[1]);

      album_name = searchStr.match(new RegExp("album_name:" + "(.*)" + ", track_number:"));
      console.log(album_name[1]);
      album_Arr.push(album_name[1]);
    }

The console logs are in the image below. You can see part of the 'searchStr' printed, along with the data types, artist name, track IDs, etc but for some reason, it says that 'searchStr' is undefined?

Console

I'm quite new to Javascript so my apologies if there is something basic I'm forgetting.


Solution

  • Multiple issues with code. Please clean up code. Sample is given below.

    function find(refill_playlist) {
      const refill_playlist_len = refill_playlist.length
      let artist_Arr = []
      let track_id_Arr = []
      let track_Arr = []
      let album_Arr = []
      for (i = 0; i < refill_playlist_len; i++) {
        var searchStr = refill_playlist[i];
        if(!searchStr) continue;
        //grab variables
        artist_name = searchStr.match(/artist_name:(.*), album_name:/);
        artist_name && artist_Arr.push(artist_name[1]);
    
        track_name = searchStr.match(/track_name:(.*), acousticness:/);
        track_name && track_Arr.push(track_name[1]);
    
        track_id = searchStr.match(/track_id:(.*), track_name:/);
        track_id && track_id_Arr.push(track_id[1]);
    
        album_name = searchStr.match(/album_name:(.*), track_number:/);
        album_name && album_Arr.push(album_name[1]);
      }
      console.log(artist_Arr)
      console.log(track_id_Arr)
      console.log(track_Arr)
      console.log(album_Arr)
    }
    find(
      [
        `
        artist_name: test, album_name:
        `,
        null
      ]
    )