我有一个二维数组

var years = [[-1,"0 years"], [1,"< 1 year"], [2,"1-3 years"], [3,"> 3 years"]];


我想根据用户输入获得年数,即-1,1,2,3如何用Java编写?

我正在尝试使用years[years_input],但没有得到正确的值。

最佳答案

您可以使用find()方法。



var years = [[-1,"0 years"], [1,"< 1 year"], [2,"1-3 years"], [3,"> 3 years"]];

let input = +prompt('enter the index');
console.log(years.find(x => x[0] === input)[1])

关于javascript - 根据二维数组中的输入获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56360816/

10-11 13:17