获取JavaScript数组下一步,previous元素 [英] Get Next and Previous Elements in JavaScript array
问题描述
我有一个大阵,与非顺序ID,这看起来是这样的:
I have a large array, with non-sequential IDs, that looks something like this:
PhotoList[89725] = new Array();
PhotoList[89725]['ImageID'] = '89725';
PhotoList[89725]['ImageSize'] = '123';
PhotoList[89726] = new Array();
PhotoList[89726]['ImageID'] = '89726';
PhotoList[89726]['ImageSize'] = '234';
PhotoList[89727] = new Array();
PhotoList[89727]['ImageID'] = '89727';
PhotoList[89727]['ImageSize'] = '345';
Etc....
我想弄清楚,给定一个ID,我如何能获得下和previous ID ...所以,我可以做这样的事情:
I'm trying to figure out, given an ID, how can I can get the next and previous ID... So that I could do something like this:
<div id="current">Showing You ID: 89726 Size: 234</div>
Get Prev Get Next
显然,如果我们在数组的结束或开始我们只是一个消息...
Obviously, if we're at the end or beginning of the array we just a message...
推荐答案
根据您的例子中,ID是连续的...
这是写你的例子的另一种方式。新的Array()真的不是你应该使用什么,因为这些都是你创建的对象。另外,我离开了数字字符串,但我不知道你为什么会想这样做。您可以添加下一个和$ P $光伏喜欢kuy建议
Based on your example the IDs are sequential... This is another way of writing your example. new Array() really isn't what you should be using because those are objects you are creating. Also, I left the numbers as strings, but I'm not sure why you would want to do that. You could add next and prev like kuy suggested
PhotoList[89725] = {ImageID: '89725',
ImageSize: '123'};
PhotoList[89725] = {ImageID: '89726',
ImageSize: '234',
Next: '89727',
Prev: '89725'};
PhotoList[89725] = {ImageID: '89727',
ImageSize: '345'};
所有的这些都只是喜欢你的其他结构的访问。
All of these are accessible just like your other structure.
这篇关于获取JavaScript数组下一步,previous元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!