简体   繁体   English

给定索引获取数组的下一个和上一个元素

[英]Get the next and previous element of an array given an index

Given I have an array in Javascript, with values as such; 鉴于我在Javascript中有一个数组,其值如此;

0     => 0x0000FF
1200  => 0x00CCFF
28800 => 0xFF0AFF
36000 => 0xFFFFFF

How can I determine which elements a given index value falls between? 如何确定给定索引值介于哪些元素之间? With the previous example, if I have the value 31073, I need to retrieve 28800 => 0xFF0AFF and 36000 => 0xFFFFFF 在前面的例子中,如果我有值31073,我需要检索28800 => 0xFF0AFF36000 => 0xFFFFFF

There's no "built-in" way to accomplish this with Javascript's sparse arrays. 使用Javascript的稀疏数组没有“内置”方法来实现这一点。 The simplest way to do this for arbitrary sparse indexes while maintaining some efficiency is to keep another lookaside sorted array of the indexes into the main array. 在保持一定效率的同时对任意稀疏索引执行此操作的最简单方法是将另一个可视化排序的索引数组保存到主数组中。 Then you can walk the lookaside list to find the right neighbor indexes, and go back to the main array to get their values. 然后,您可以遍历旁视列表以查找正确的邻居索引,然后返回主数组以获取其值。

If your array will be huge or accesses need to be faster than O(items), you could look into various tree structures for the lookaside object. 如果你的数组很大或者访问需要比O(项目)快,你可以查看旁视对象的各种树结构。

Here's one way that simply uses a couple while() loops with no body. 这是一种简单地使用一对没有正文的while()循环的方法。

It assumes the starting point will always be in between. 它假设起点始终介于两者之间。 You'll need a couple of quick additional tests if that's not the case. 如果不是这样的话,你需要几个快速的额外测试。

Also, I wasn't sure what should happen if the starting point is directly on a color, so I didn't account for that. 此外,我不确定如果起点直接在一个颜色上会发生什么,所以我没有考虑到这一点。

Example: http://jsfiddle.net/tcVxP/4/ 示例: http //jsfiddle.net/tcVxP/4/

var num = 31234,
    curr = num,
    prev,
    next;

while( !colors[--curr] && curr );
prev = colors[curr];

curr = num;
while( !colors[++curr] );
next = colors[curr];

I just wanted to follow up here; 我只是想在这里跟进;

Thanks to both quixoto and patrick dw for your detailed comments and answers. 感谢quixotopatrick dw的详细评论和答案。 I have however, gone with a slightly different solution. 但是,我的解决方案略有不同。 While I wanted to maintain my initial approach of using a one dimensional array, it's much easier and efficient from what I can see, to add another dimension as shown below. 虽然我想保持我使用一维数组的初始方法,但从我所看到的更容易和有效,添加另一个维度,如下所示。 The array used here does exhibit predictability, but that may not be the case once the project is finalized. 这里使用的数组确实具有可预测性,但一旦项目完成,情况可能并非如此。

var colors = [  
    [0,     '121D4A'],
    [10800, '000000'],
    [21600, 'FF5900'],
    [32400, 'D3EEF0'],
    [43200, '7DCDFF'],
    [54000, '7DA6FF'],
    [64800, 'FF5900'],
    [75600, '31428C'],
    [86399, '121D4A'],
];

function gradientStop(color1, color2, gradStop){
    var r = Math.floor(gradStop * parseInt(color2.substr(0, 2), 16) + (1 - gradStop) * parseInt(color1.substr(0, 2), 16)).toString(16);
    var g = Math.floor(gradStop * parseInt(color2.substr(2, 2), 16) + (1 - gradStop) * parseInt(color1.substr(2, 2), 16)).toString(16);
    var b = Math.floor(gradStop * parseInt(color2.substr(4, 2), 16) + (1 - gradStop) * parseInt(color1.substr(4, 2), 16)).toString(16);
    return (r.length < 2 ? '0' + r : r) + (g.length < 2 ? '0' + g : g) + (b.length < 2 ? '0' + b : b);
}

function getColor(colors, currentIndex){
    for(var i = 0, m = colors.length; i < m; i++){
        if(currentIndex >= colors[i][0]){
            if(typeof(colors[i + 1]) !== 'undefined'){
                if(currentIndex <= colors[i + 1][0]){
                    return gradientStop(colors[i][1], colors[i + 1][1], (currentIndex - colors[i][0]) / (colors[i + 1][0] - colors[i][0]));
                }
            }
        }
    }
}

And also; 并且; Yes Hemlock it was an expansion on my question at Programmatic gradient stops with Javascript , hence the gradientStop() function. 是的, Hemlock是我在使用Javascript编程渐变停止的问题上的扩展,因此是gradientStop()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM