Моя цель - чтобы пользователь щелкнул по дате, и карта США изменится, чтобы использовать новый массив данных, основанный на выбранном году.
// When user clicks on li under years
$('#years').children('li').click(function(){
// Get last two characters of id and prepend y
let id = 'y' + this.id.slice(-2);
// Output is y04
console.log(id);
// Use id variable to get array for that year, then pass through getColour function.
let newArray = $.map(id, function(item) {
return getColour("#e67d73", "#57bb8a", 16, 100, item)
});
// step through states array and apply the colour value from newArray to each state
states.forEach((state, index) => {
const colour = newArray[index];
$(state).css('fill', colour);
});
});
Для начальной загрузки карты я с помощью. Это прекрасно работает:
//Use y04 array for getColour function
let initialArray = y04.map(function(item) {
return getColour("#e67d73", "#57bb8a", 16, 100, item)
});
Я провел различные тесты с использованием let newArray = id.map(//more code)
, но из моих исследований переменная id
должна быть объектом, а не строкой.
Как я могу преобразовать мою id
строку в объект, который .map
может использовать? Можно ли передать переменную в качестве исходного массива в .map
?