python中enumerate在for循环中用法_【已解决】js中对于数组的for循环中是否有类似于Python中的enumerate...

折腾:

期间,想要对于js中的数组:

去for循环,同时希望获取index

所以想到了:

js中是否有python中的,对于数组之类的enumerate

js enumerate

Array.prototype.entries()

但是依旧不太好用:

for(let curIdx, curFunctionGroupName of functionGroupNames.entries()){

会报错。

所以继续搜:

js array entries example

Object.entries(user) = [ ["name","John"], ["age",30] ]

感觉应该写成:for中每个都是 keyValueList

后来自己去回复了:

for(let curIndexValueList of someArray.entries()){

console.log("curIndexValueList=", curIndexValueList)

let curIndex = curIndexValueList[0]

let curValue = curIndexValueList[1]

console.log("curIndex=", curIndex, ", curValue=", curValue)

}

等价于python代码:

for curIndex, curValue in enumerate(someArray):

print("curIndex=%s, curValue=%s" % (curIndex, curValue))

}

【总结】

最后用代码:

if (functionGroupNames && functionGroupOwners && functionGroupIDs) {

let functionGroupInfoDictList = []

for(let curKeyValueList of functionGroupNames.entries()){

console.log("curKeyValueList=", curKeyValueList)

let curIdx = curKeyValueList[0]

let curGroupName = curKeyValueList[1]

console.log("curIdx=", curIdx, "curGroupName=", curGroupName)

let curGroupOwner = functionGroupOwners[curIdx]

let curGroupId = functionGroupIDs[curIdx]

console.log("curGroupOwner=", curGroupOwner, "curGroupId=", curGroupId)

let curGroupInfoDict = {

"groupId": curGroupId,

"groupName": curGroupName,

"groupOwnerName": curGroupOwner,

}

functionGroupInfoDictList.push(curGroupInfoDict)

}

console.log("functionGroupInfoDictList=", functionGroupInfoDictList)

}

核心部分是:

for(let curKeyValueList of functionGroupNames.entries()){

let curIdx = curKeyValueList[0]

let curGroupName = curKeyValueList[1]

效果是:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值