js 如何获取嵌套对象的索引值?

2023-06-22 332 0

后台返回的对象如下

{
    "result": [
        {
            "cityAndPrefecture": [
                {
                    "city": "滨海新区",
                    "prefecture": [
                        "全市",
                        "天津滨海旅游区",
                        "天津东疆海水浴场"
                    ]
                }
            ],
            "province": "天津市"
        },
        {
            "cityAndPrefecture": [
                {
                    "city": "东营",
                    "prefecture": []
                },
                {
                    "city": "威海",
                    "prefecture": []
                },
                {
                    "city": "日照",
                    "prefecture": [
                        "全市",
                        "日照桃花岛"
                    ]
                },
                {
                    "city": "滨州",
                    "prefecture": []
                },
                {
                    "city": "潍坊",
                    "prefecture": [
                        "全市",
                        "潍坊度假区"
                    ]
                },
                {
                    "city": "烟台",
                    "prefecture": [
                        "全市",
                        "海阳万米海滩",
                        "金沙滩海水浴场(烟台)",
                        "屺坶岛"
                    ]
                },
                {
                    "city": "青岛",
                    "prefecture": [
                        "全市",
                        "第一海水浴场",
                        "第六海水浴场",
                        "石老人海水浴场",
                        "金沙滩海水浴场(青岛)"
                    ]
                }
            ],
            "province": "山东省"
        },
        {
            "cityAndPrefecture": [
                {
                    "city": "唐山",
                    "prefecture": []
                },
                {
                    "city": "沧州",
                    "prefecture": []
                },
                {
                    "city": "秦皇岛",
                    "prefecture": [
                        "全市",
                        "北戴河老虎石浴场",
                        "南戴河浴场",
                        "东山浴场",
                        "西浴场",
                        "北戴河旅游区"
                    ]
                }
            ],
            "province": "河北省"
        },
        {
            "cityAndPrefecture": [
                {
                    "city": "丹东",
                    "prefecture": []
                },
                {
                    "city": "大连",
                    "prefecture": [
                        "全市",
                        "棒棰岛浴场",
                        "付家庄浴场",
                        "星海浴场",
                        "夏家河浴场",
                        "泊石湾",
                        "金石滩",
                        "仙浴湾",
                        "大黑石",
                        "塔河湾"
                    ]
                },
                {
                    "city": "盘锦",
                    "prefecture": []
                },
                {
                    "city": "营口",
                    "prefecture": []
                },
                {
                    "city": "葫芦岛",
                    "prefecture": []
                },
                {
                    "city": "锦州",
                    "prefecture": []
                }
            ],
            "province": "辽宁省"
        }
    ],
    "success": true
}

传入一个值,如果这个值是city的值 就返回 city province 的索引值 传入是prefecture的值 就返回 prefecture city province的值
[0,1] 或者 [0,2,3]这种

function getIndexChain(value, list) {
    let result = []
    list.some((item, index) => {
        if(item.province === value){
            result.push(index)
            return true
        }else{
            return item.cityAndPrefecture.some((citem, cindex) => {
                if(citem.city === value){
                    result.push(cindex, index)
                    return true
                }else{
                    let i = citem.prefecture.indexOf(value)
                    if(i >= 0){
                        result.push(i, cindex, index)
                        return true
                    }
                }
            })
        }
    })
    return result
}
getIndexChain('日照桃花岛', list) // [1, 2, 1]
getIndexChain('东营', list) // [0, 1]

image.png


  function getValueByType (type) {
    if (type === "city") {
      return [0, 1]; 
    } else if (type === "prefecture") {
      const values = [];
      for (const item of data.result) {
        for (const cp of item.cityAndPrefecture) {
          values.push(...cp.prefecture); 
        }
      }
      values.unshift("全市"); 
      return values; 
    } else {
      return []; 
    }
  }
  console.log(getValueByType("city")); 

image.png

const getIndexArr = (str) => {
  const { result } = a;
  // 考虑到可能会传'全市'等 产生多个index集合
  const arr = [];
  for (const index in result) {
    const { cityAndPrefecture, province } = result[index];
    if (province === str) arr.push([index]);
    for (const index2 in cityAndPrefecture) {
      const { city, prefecture } = cityAndPrefecture[index2];
      if (city === str) arr.push([index, index2]);
      for (const index3 in prefecture) {
        if (prefecture[index3] === str) arr.push([index, index2, index3]);
      }
    }
  }
  return arr;
};
console.log(getIndexArr("全市"));
console.log(getIndexArr("塔河湾"));

回答

相关文章

nuxt2部署静态化和ssr的时候访问首页先报404再出现首页为什么?
`clip-path` 如何绘制圆角平行四边形呢?
多线程wait方法报错?
VUE 绑定的方法如何直接使用外部函数?
vue2固定定位该怎么做?
谁有redis实现信号量的代码,希望借鉴一下?