简体   繁体   English

将数字值分配给字符串数组

[英]Assigning Number Values to String Array

I am trying to find a way to make an array with strings have values such as [1, 2, 3, 4,...] and so on.I believe I saw a way to do it utilizing 'indexOf + 1' thanks for any help. 我正在尝试找到一种使字符串数组具有[1、2、3、4,...]等值的方法。我相信我看到了一种利用'indexOf + 1'的方法来完成此工作的方法,谢谢任何帮助。

function sortGrades(lst){
var grades = ["VB", "V0", "V0+","V1", "V2", "V3", "V4", "V5", "V6", "V7", 
"V8",` "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17"];

 }

Perhaps something like this? 也许像这样?

grades.map(function(v,i) { return i + 1; });
// Or cleaner but ES5:
grades.map((v,i) => i+1)

Even though I might have misunderstood your question. 即使我可能误解了您的问题。

You can simply using map : 您可以简单地使用map

 var grades = ["VB", "V0", "V0+","V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17"]; var numericGrades = grades.map((grade, index) => index + 1); console.log(numericGrades); 

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

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