当前位置: 首页 > 知识库问答 >
问题:

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

步联
2023-06-16

后台返回的对象如下

{
    "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]这种

共有3个答案

周浩博
2023-06-16

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("塔河湾"));
柳昊焱
2023-06-16

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")); 
南门向荣
2023-06-16
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]
 类似资料:
  • 问题内容: 我在下面的对象中尝试获取所有id值。 使用以下代码,我仅获得第一个id值。有没有办法从嵌套对象中获取所有id值,而无需使用任何外部模块。 预期产量 问题答案: 您可以使用如下所示的JavaScript函数来获取嵌套属性: 检查此小提琴以获取可行的解决方案。

  • 问题内容: 我有一个包含嵌套对象的文档,如下所示: 现在,我需要按书名(不是book_title)和年份(比如2014)来过滤书籍。我需要的输出将是: 当我使用嵌套过滤器时,即使它们不匹配,我也会得到所有嵌套对象。如何仅获取匹配的嵌套对象? 问题答案: 您需要使用以下嵌套功能。 在输出中,您将确切地获得期望的结果,即字段和嵌套数组中的匹配书。

  • 我有一个2D按钮数组(按钮),它将生成一个5×5的按钮网格。我想在单击时获取单个按钮的索引(例如,(2,2)在中间),并通过创建Topcenter(这将是按钮的索引(x,y-1)或1,2,其中x和y是单击按钮的值)等整数变量,在3 x 3半径内找到围绕原始按钮的按钮的索引值。)然后,我可以向周围的按钮添加文本等。 这是一个可视化: 0,0|0,1|0,2|0,3|0,4 1,0 | 1,1 | 1

  • 问题内容: 例如,我有: 然后,例如,我要通过对该对象进行 排序/反转。然后我想得到这样的东西: 现在,我想知道具有属性的对象的索引,以获取属性标记的值。 我该如何解决这个问题? 问题答案: 正如其他答案所暗示的那样,遍历数组可能是最好的方法。但是我会把它放在它自己的函数中,并使它更抽象一些: 这样,您不仅可以找到包含“ John”的一个,而且可以找到包含令牌“ 312312”的一个: 编辑: 未

  • 问题内容: 我创建了一个JavaScript对象,但是如何确定该对象的类呢? 我想要一些类似于Java的方法。 问题答案: JavaScript中没有Java的完全对应版本。通常,这是由于JavaScript是一种基于原型的语言,而不是Java是一种基于类的语言。 一些例子: 注意:如果使用Uglify编译代码,它将更改非全局类名。为了防止这种情况,Uglify有一个参数,可以使用gulp或gru

  • 问题内容: 我试图遍历嵌套对象以检索由字符串标识的特定对象。在下面的示例对象中,标识符字符串是“ label”属性。我无法解决如何遍历树以返回适当对象的问题。任何帮助或建议,将不胜感激。 问题答案: 您可以创建像这样的递归函数来对对象进行深度优先遍历。 可以这样称呼