rob.m rob.m - 1 month ago 17
Javascript Question

t.replace is not a function(…)trim

The following is producing the error in the question when I check in console.log and it breaks it

var map = L.map('map').setView([0, 0], 2);

<?php
$classesForCountries = [];
if (have_posts()) : while (have_posts()) : the_post();
$classesForCountries[ get_field('country') ] += get_field('year') + ' ';
endwhile; endif;
?>

// Now this should look something like {"Australia": "2006 2010 "}

var classNameMap = <?php echo JSON_encode($classesForCountries); ?>;
geojson = L.geoJson(statesData, {
style: function(feature) {

// Here is where we got the issue

var classes = classNameMap[feature.properties.sovereignt];
if (classes) {
return {className: classes};
}
},
}).addTo(map);


UPDATE

by looking at console.log, it points me to a line in the library:

...t.replace(/^\s+|\s+$/g,"")},splitWords:function(t){return o.Util.trim(t).split(/\s+/)}...

Answer

"@Fred-ii- lol superb. Thanks a lot. Put that in an answer and I will accept it. – rob.m"

As per OP's request:

This += get_field('year') + ' '

You may have come from a JS/C background and think that the + signs can be used for concatenation in PHP. PHP is interpreting the plus signs in thinking you want to do math.

It is the dots that concatenate in PHP:

.= get_field('year') . ' '