Here is a library that solves this exact problem:
https://github.com/anomal/RainbowVis-JS
So after including it you would do something like:
var rainbow = new Rainbow();
rainbow.setSpectrum('#ffffff', '#3E296B');
rainbow.setNumberRange(1, 20);
rainbow.colourAt(number); // based on the numbers from your array, this would return the color you want