0

有这样的数据结构

{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 黑龙江省哈尔滨市鸡东县

我应该如何去设计这个算法,更小的复杂度?

2019-06-14 提问
2 个回答
0
let data = [230000,230100,230321]
let PArr = [] // 所有省
let pIndex = PArr.findIndex(item => item.value === data[0])
let CArr = PArr[pIndex].children
let cIndex = CArr.findIndex(item => item.value === data[1])
...
以此类推,找到所有对应的index,然后你应该会了
0
{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]

撰写答案

推广链接