最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Returning an evenly "spaced" number of indexes from a large array. - Stack Overflow

programmeradmin0浏览0评论

I have a Javascript array of 20 RGB color values that looks like this:

defaultColors: [ rgb(58, 185, 180)','rgb(63, 186, 172)','rgb(71, 185, 159)','rgb(80, 185, 146)','rgb(90, 186, 132)','rgb(103, 187, 119)','rgb(117, 188, 104)','rgb(137, 193, 96)','rgb(163, 200, 90)','rgb(189, 206, 87)','rgb(212, 214, 86)','rgb(232, 219, 87)','rgb(245, 221, 89)','rgb(254, 221, 87)','rgb(254, 216, 83)','rgb(254, 206, 78)', 'rgb(253, 193, 72)','rgb(251, 178, 66)','rgb(244, 163, 63)','rgb(240, 150, 60)']

At any given time I may only need, say, 7 of these colors, but I want to pull colors from the full spectrum. I don't just want the first 7 colors. I'd need something more like this:

Or say I need 10 colors. That would look more like this:

Suggestions around how to pull this off?

I have a Javascript array of 20 RGB color values that looks like this:

defaultColors: [ rgb(58, 185, 180)','rgb(63, 186, 172)','rgb(71, 185, 159)','rgb(80, 185, 146)','rgb(90, 186, 132)','rgb(103, 187, 119)','rgb(117, 188, 104)','rgb(137, 193, 96)','rgb(163, 200, 90)','rgb(189, 206, 87)','rgb(212, 214, 86)','rgb(232, 219, 87)','rgb(245, 221, 89)','rgb(254, 221, 87)','rgb(254, 216, 83)','rgb(254, 206, 78)', 'rgb(253, 193, 72)','rgb(251, 178, 66)','rgb(244, 163, 63)','rgb(240, 150, 60)']

At any given time I may only need, say, 7 of these colors, but I want to pull colors from the full spectrum. I don't just want the first 7 colors. I'd need something more like this:

Or say I need 10 colors. That would look more like this:

Suggestions around how to pull this off?

Share Improve this question asked Feb 20, 2012 at 17:10 Brandon DurhamBrandon Durham 7,72713 gold badges66 silver badges113 bronze badges 1
  • One thing I notice is that you're missing a single quote mark before the first color in the array. Other than that, is this something you could do with a for loop? You could just vary the increment value of the loop index so it only grabs every nth item from the array... – Darek Rossman Commented Feb 20, 2012 at 17:19
Add a ment  | 

3 Answers 3

Reset to default 4

When you need 7, evenly spaced, Math.ceil(20/7) = 3, so you can do:

var chosenColors = [];
for(var i =0; i<20 && chosenColors.length<7; i+=3) chosenColors.push(defaultColors[i]);

When you need 10, evenly spaced, Math.ceil(20/10) = 2, so you can do:

var chosenColors = [];
for(var i =0; i<20 && chosenColors.length<10; i+=2) chosenColors.push(defaultColors[i]);
// pick n elements from a, distibuted evenly
pickn = function(a, n) {
    var p = Math.floor(a.length / n)
    return a.slice(0, p * n).filter(function(_, i) { 
        return 0 == i % p
    })
}

// test

test = [0,11,22,33,44,55,66,77,88,99,111,222,333,444,555,666,777,888,999]
for (i = 1; i < test.length; i++)
    console.log(i, pickn(test, i))
var defaultColors= [ 'rgb(58, 185, 180)','rgb(63, 186, 172)','rgb(71, 185, 159)','rgb(80, 185, 146)','rgb(90, 186, 132)','rgb(103, 187, 119)','rgb(117, 188, 104)','rgb(137, 193, 96)','rgb(163, 200, 90)','rgb(189, 206, 87)','rgb(212, 214, 86)','rgb(232, 219, 87)','rgb(245, 221, 89)','rgb(254, 221, 87)','rgb(254, 216, 83)','rgb(254, 206, 78)', 'rgb(253, 193, 72)','rgb(251, 178, 66)','rgb(244, 163, 63)','rgb(240, 150, 60)'];

var need = 10;


for (var i=0, every=defaultColors.length/need|0;i<defaultColors.length;i+=every) {
  console.log(defaultColors[i]);
}
发布评论

评论列表(0)

  1. 暂无评论