猿问

跪求!一道实战开发遇到的递归题目各位有什么建议?

有这样的数据结构
{value:"230000",label:"黑龙江省",pk:"1001Z01000000000SGGS",children:Array(13)}
children:
{value:"230100",label:"哈尔滨市",pk:"1001Z01000000000SGGT",children:Array(14)}
{value:"230200",label:"齐哈尔市",pk:"1001Z01000000000SGHD",children:Array(10)}
{value:"230300",label:"鸡西市",pk:"1001Z01000000000SGHV",children:Array(4)}
children:
{value:"230301",label:"市辖区",pk:"1001Z01000000000SGHW",children:Array(6)}
{value:"230321",label:"鸡东县",pk:"1001Z01000000000SGI3"}
{value:"230381",label:"虎林市",pk:"1001Z01000000000SGI4"}
{value:"230382",label:"密山市",pk:"1001Z01000000000SGI5"}
我有一个value数组.例如[230000,230100,230321]最后我要最后对应的label黑龙江省哈尔滨市鸡东县
我应该如何去设计这个算法,更小的复杂度?
噜噜哒
浏览 229回答 2
2回答

心有法竹

letdata=[230000,230100,230321]letPArr=[]//所有省letpIndex=PArr.findIndex(item=>item.value===data[0])letCArr=PArr[pIndex].childrenletcIndex=CArr.findIndex(item=>item.value===data[1])...以此类推,找到所有对应的index,然后你应该会了

一只甜甜圈

{value:"230000",label:"黑龙江省",pk:"1001Z01000000000SGGS",children:Array(13)}children:{value:"230100",label:"哈尔滨市",pk:"1001Z01000000000SGGT",children:Array(14)}{value:"230200",label:"齐哈尔市",pk:"1001Z01000000000SGHD",children:Array(10)}{value:"230300",label:"鸡西市",pk:"1001Z01000000000SGHV",children:Array(4)}其实,不能改下数据的结构吗?{230000:{value:"230000",label:"黑龙江省",pk:"1001Z01000000000SGGS",children:Object}children:{230100:{value:"230100",label:"哈尔滨市",pk:"1001Z01000000000SGGT",children:Object}230200:{value:"230200",label:"齐哈尔市",pk:"1001Z01000000000SGHD",children:Object}230300:{value:"230300",label:"鸡西市",pk:"1001Z01000000000SGHV",children:Object}}}找[230000,230100,230321],object[230000]['children'][230100]['children'][230321]
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答