Javascript loaded via DrupalSettings variable not executing? I have added a js library and associated code to get all Location nodes onto a Map page

This code works fine if I paste it into the map.js file but if I use the drupalSettings variable, the code is appearing in the map.js file but doesn't appear to be executing

The map.js file

(function ($, Drupal, drupalSettings) {

    'use strict';

    Drupal.behaviors.mybehavior = {
        attach: function (context, settings) {

            var map = L.map('mapid').setView([-38.037511118801255, 176.41345152203002], 13);

            var googleSat = L.tileLayer('http://{s}.google.com/vt/lyrs=s&amp;x={x}&amp;y={y}&amp;z={z}',{
                maxZoom: 20,
                subdomains:['mt0','mt1','mt2','mt3']
            }).addTo(map);

            // the data from basic.theme file, basic_page_attachments_alter function
            // console.log(settings.basic.nr_map.foo);
            settings.basic.nr_map.foo;

        }
    };

})(jQuery, Drupal, drupalSettings);


And the code from the theme function

function basic_page_attachments_alter(&amp;$page) {
  $node = \Drupal::routeMatch()-&gt;getParameter('node');
  if (!empty($node)) { // if empty its a view and has no bundle - causing 503
    $bundle = $node-&gt;bundle();
    if($bundle == 'map') {
      $mapid = $node-&gt;id();
      $nids = \Drupal::entityQuery('node')-&gt;condition('type','location')-&gt;execute();
      $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

      $out = '';

      foreach ($nodes as $key =&gt; $location) {
        $map_title = $location-&gt;getTitle();

        // lat, lon
        $latlong_arr = $location-&gt;get('field_mahere_location')-&gt;getValue();
        $lat = $latlong_arr[0]['lat'];
        $lon = $latlong_arr[0]['lon'];

        $out .= 'var ' . strtolower($video_title) . ' = L.popup({ closeButton: false, closeOnClick: false, autoClose: false, className: "static" }).setLatLng([' . $lat . ', ' . $lon . ']).setContent("&lt;p class=\'location static\'&gt;' . $map_title . '&lt;/p&gt;").openPopup().addTo(map);' . PHP_EOL;
      }

      $page['#attached']['drupalSettings']['basic']['nr_map']['foo'] = $out;
      $page['#attached']['library'][] = 'basic/nr_map';
    }
  }
}


If I copy the code from the console i.e. console.log(settings.basic.nr_map.foo) and paste it into the map.js file it runs perfectly and creates all of the map markers I'm after, but if it comes from the variable i.e. settings.basic.nr_map.foo it doesn't execute, and doesn't create any map markers

Any ideas? It seems like it should be simple!

Apologies for the duplicate post but I haven't had any joy on drupal forum.

Thanks

EDIT:
And the libraries yml file

nr_map: 
  js:
    js/map.js: {}
  dependencies:
    - core/drupal
    - core/jquery
    - core/drupalSettings
Use drupalSettings.basic.nr_map.foo instead of settings.basic.nr_map.foo in your JavaScript.