Javascript:通用获取数组中的下一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16294835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Generic get next item in array
提问by jak
I am trying to make a JavaScript function that will search an array of strings for a value and return the next string. For example, if an array is built such that an item is followed by its stock code, I want to search for the item and have the stock code written.
我正在尝试创建一个 JavaScript 函数,它将在字符串数组中搜索一个值并返回下一个字符串。例如,如果构建了一个数组,使得一个项目后跟其库存代码,我想搜索该项目并写入库存代码。
var item = (from user input); //some code to get the initial item from user
function findcode(code){
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
for (var i=0; i<arr.lenth; i++){ //for loop to look through array
arr.indexOf(item); //search array for whatever the user input was
var code = arr(i+1); //make the variable 'code' whatever comes next
break;
}
}
document.write(code); //write the code, I.e., whatever comes after the item
(I'm sure it's obvious I'm new to JavaScript, and while this is similar to a number of other questions I found, those seemed to have more involved arrays or more complex searches. I can't seem to simplify them for my needs.)
(我确信很明显我是 JavaScript 的新手,虽然这与我发现的许多其他问题类似,但这些问题似乎涉及更多的数组或更复杂的搜索。我似乎无法为我的问题简化它们需要。)
回答by georg
You've almost got it right, but the syntax is arr[x]
, not arr(x)
:
你几乎猜对了,但语法是arr[x]
,而不是arr(x)
:
index = array.indexOf(value);
if(index >= 0 && index < array.length - 1)
nextItem = array[index + 1]
BTW, using an object instead of an array might be a better option:
顺便说一句,使用对象而不是数组可能是更好的选择:
data = {"ball":"1f7g", "spoon":"2c8d", "pen":"9c3c"}
and then simply
然后简单地
code = data[name]
回答by SerzN1
Cycled items from array this might be useful
从数组循环项目这可能有用
const currentIndex = items.indexOf(currentItem);
const nextIndex = (currentIndex + 1) % items.length;
items[nextIndex];
The first item will be taken from the beginning of the array after the last item
第一项将从最后一项之后从数组的开头取出
回答by Fabrizio Calderan
I think that an object could be probably a better data structure for this kind of task
我认为对于此类任务,对象可能是更好的数据结构
items = {
ball : "1f7g",
spoon: "2c8d",
pen : "9c3c"
}
console.log(items['ball']); // 1f7g
回答by Richie Bendall
Try this String.prototype
function:
试试这个String.prototype
功能:
String.prototype.cycle = function(arr) {
const i = arr.indexOf(this.toString())
if (i === -1) return undefined
return arr[(i + 1) % arr.length];
};
Here is how you use it:
以下是您如何使用它:
"a".cycle(["a", "b", "c"]); // "b"
"b".cycle(["a", "b", "c"]); // "c"
"c".cycle(["a", "b", "c"]); // "a"
"item1".cycle(["item1", "item2", "item3"]) // "item2"
If you want to do it the other way round, you can use this Array.prototype
function:
如果你想反过来做,你可以使用这个Array.prototype
函数:
Array.prototype.cycle = function(str) {
const i = this.indexOf(str);
if (i === -1) return undefined;
return this[(i + 1) % this.length];
};
Here is how you use it:
以下是您如何使用它:
["a", "b", "c"].cycle("a"); // "b"
["a", "b", "c"].cycle("b"); // "c"
["a", "b", "c"].cycle("c"); // "a"
["item1", "item2", "item3"].cycle("item1") // "item2"
回答by Vadim
You may pass array to function as argument and return found value from function:
您可以将数组作为参数传递给函数并从函数返回找到的值:
var item = "spoon"; // from user input
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
function findcode(item, arr){
var idx = arr.indexOf(item); //search array for whatever the user input was
if(idx >=0 && idx <= arr.length - 2) { // check index is in array bounds
return arr[i+1]; // return whatever comes next to item
}
return '';
}
document.write(findcode(item, arr)); //write the code, i.e., whatever comes after the item