【问题标题】:get index of all objects in a json array获取 json 数组中所有对象的索引
【发布时间】:2017-08-25 18:27:56
【问题描述】:

这是我的 json::

[{ “名称”:“罗伊”, “城市”:“达拉斯”, “州”:“德克萨斯州” }, { “名称”:“卡拉”, “城市”:“洛杉矶”, “州”:“加利福尼亚” },{ “名称”:“菲利克斯”, “城市”:“拉斯维加斯”, “州”:“内华达州” },{ “名称”:“弗雷德”, “城市”:“迈阿密”, “州”:“佛罗里达” },{ “姓名”:“比尔”, “城市”:“亚特兰大”, “州”:“格鲁吉亚” },{ “名称”:“迈克”, “城市”:“芝加哥”, “州”:“伊利诺伊州” },{ “名称”:“蒂姆”, “城市”:“伍斯特”, “状态”:“MA” },{ “名称”:“瑞恩”, “城市”:“奥斯汀”, “州”:“德克萨斯州” },{ “名称”:“山姆”, “城市”:“波士顿”, “状态”:“MA” },{ “名称”:“莎拉”, “城市”:“休斯顿”, “州”:“德克萨斯州” }]

我想捕获德克萨斯州的所有索引号:

请帮忙。

【问题讨论】:

  • 你能具体说明你在做什么吗? JSON解析?循环逻辑?两者的结合?
  • 循环逻辑。我不想得到所有的索引号。

标签: javascript jquery


【解决方案1】:

您可以在收集匹配状态的索引时使用Array#reduce

var array = [{ name: "Roy", city: "Dallas", state: "Texas" }, { name: "Karla", city: "LA", state: "California" }, { name: "Felix", city: "Las Vegas", state: "Nevada" }, { name: "Fred", city: "Miami", state: "Florida" }, { name: "Bill", city: "Atlanta", state: "Georgia" }, { name: "Mike", city: "Chicago", state: "Illinois" }, { name: "Tim", city: "Worcester", state: "MA" }, { name: "Ryan", city: "Austin", state: "Texas" }, { name: "Sam", city: "Boston", state: "MA" }, { name: "Sarah", city: "Houston", state: "Texas" }],
    indices = array.reduce((r, o, i) => (o.state === 'Texas' && r.push(i), r), []);

console.log(indices);

【讨论】:

    【解决方案2】:

    您可以使用map()filter() 方法,如下所示。

    var data = [{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }];
    
    var indexes = data.map(function(item, i){
        if(item.state == "Texas") return i;
    }).filter(function(item){ return item!=undefined; });
    
    console.log(indexes);

    【讨论】:

      【解决方案3】:

      您可以枚举集合并将索引添加到数组中,其中 state 为“Texas”。如果您使用的是服务器端代码,则可以使用 JSON 解析器反序列化响应(到包含与 JSON 对象相同属性的目标类型)。如果您使用的是Javascript,您可以将JSON.parse(jsonString) 转换为对象数组然后枚举

      【讨论】:

        【解决方案4】:

        您可以使用 for 循环来实现此目的。迭代一个变量,比如“i”,会很有用。

        texas_indecies = [];
        for (var i = 0; i < array.length; i++){
          if (array[i].state == "Texas"){
            texas_indecies.push(i);
          }
        }
        

        【讨论】:

          【解决方案5】:

          这是你需要的吗?

          var obj=[{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }],
          indexes=[];
          
          for(var index in obj){
            if(obj[index]['state']=='Texas')
              indexes.push(index);
          }
          
          console.log(indexes);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-10-23
            • 2021-03-17
            • 1970-01-01
            • 1970-01-01
            • 2012-02-21
            • 1970-01-01
            • 2020-05-04
            • 2017-06-16
            相关资源
            最近更新 更多