【问题标题】:search inside child Array of objects - JavaScript在子对象数组中搜索 - JavaScript
【发布时间】:2019-02-12 11:34:16
【问题描述】:

目前我有以下对象数组

obj = [
    {
        "id":28,
        cities: [
            {
                cityTypes: "AA",
                citySource: "sdsf"
            },
            {
                cityTypes: "BB",
                citySource: "sdsgf"
            },
            {
                cityTypes: "CC",
                citySource: "fgsdfgd"
            }
        ]
    }, 
    {
        "id":56,
        cities: [
            {
                cityTypes: "DD",
                citySource: "sdsf"
            },
            {
                cityTypes: "EE",
                citySource: "sdsgf"
            },
            {
                cityTypes: "FF",
                citySource: "fgsdfgd"
            }
        ]
    }, 
    {
        "id":89,
        cities: [
            {
                cityTypes: "GG",
                citySource: "sdsf"
            },
            {
                cityTypes: "HH",
                citySource: "sdsgf"
            },
            {
                cityTypes: "II",
                citySource: "fgsdfgd"
            }
        ]
    }
]

我需要在整个Object 中搜索特定值的cityTypes

假设,我需要搜索cityTypes = BB

如果BB存在于整个对象中,则返回true

如果BB没有预设,返回false

这是我尝试过的,但似乎不起作用。

for(let k=0; k<obj.length; k++){
    if(obj[k].cities){
        let cityObj = obj[k].cities;
        for(let city in cityObj){
            city.cityTypes !== "BB" ? "true" : "false"
        }
    }
}

实现这一目标的正确方法是什么?

【问题讨论】:

    标签: javascript arrays loops object search


    【解决方案1】:

    您可以在另一个.some 中使用.some

    const obj=[{"id":28,cities:[{cityTypes:"AA",citySource:"sdsf"},{cityTypes:"BB",citySource:"sdsgf"},{cityTypes:"CC",citySource:"fgsdfgd"}]},{"id":56,cities:[{cityTypes:"DD",citySource:"sdsf"},{cityTypes:"EE",citySource:"sdsgf"},{cityTypes:"FF",citySource:"fgsdfgd"}]},{"id":89,cities:[{cityTypes:"GG",citySource:"sdsf"},{cityTypes:"HH",citySource:"sdsgf"},{cityTypes:"II",citySource:"fgsdfgd"}]}];
    
    console.log(
      obj.some(({ cities }) => cities.some(({ cityTypes }) => cityTypes === 'BB'))
    );
    console.log(
      obj.some(({ cities }) => cities.some(({ cityTypes }) => cityTypes === 'foobar'))
    );

    【讨论】:

      【解决方案2】:

      由于以下几个原因,您现有的代码无法正常工作:

      1. 您需要执行let city of cityObj,因为您在内部循环中以city.cityTypes 的形式访问city 的属性,因此使用of 将为您提供city 变量中的每个对象。
      2. 您需要使用if 条件实际检查是否找到匹配项。如果找到与您期望的 cityTypes 匹配,则 break 内部循环。如果找到匹配项,还要break 外循环。

      var obj = [{
          "id": 28,
          cities: [{
              cityTypes: "AA",
              citySource: "sdsf"
            },
            {
              cityTypes: "BB",
              citySource: "sdsgf"
            },
            {
              cityTypes: "CC",
              citySource: "fgsdfgd"
            }
          ]
        },
        {
          "id": 56,
          cities: [{
              cityTypes: "DD",
              citySource: "sdsf"
            },
            {
              cityTypes: "EE",
              citySource: "sdsgf"
            },
            {
              cityTypes: "FF",
              citySource: "fgsdfgd"
            }
          ]
        },
        {
          "id": 89,
          cities: [{
              cityTypes: "GG",
              citySource: "sdsf"
            },
            {
              cityTypes: "HH",
              citySource: "sdsgf"
            },
            {
              cityTypes: "II",
              citySource: "fgsdfgd"
            }
          ]
        }
      ]
      var found = false;
      for (let k = 0; k < obj.length; k++) {
        if (obj[k].cities) {
          let cityObj = obj[k].cities;
          for (let city of cityObj) {
            found = city.cityTypes === "BB";
            if (found) {
              break;
            }
          }
        }
        if (found) {
          break;
        }
      }
      console.log(found);

      【讨论】:

        【解决方案3】:

        使用双重减少应该可以工作

        var obj = [{
            "id": 28,
            cities: [{
                cityTypes: "AA",
                citySource: "sdsf"
              },
              {
                cityTypes: "BB",
                citySource: "sdsgf"
              },
              {
                cityTypes: "CC",
                citySource: "fgsdfgd"
              }
            ]
          },
          {
            "id": 56,
            cities: [{
                cityTypes: "DD",
                citySource: "sdsf"
              },
              {
                cityTypes: "EE",
                citySource: "sdsgf"
              },
              {
                cityTypes: "FF",
                citySource: "fgsdfgd"
              }
            ]
          },
          {
            "id": 89,
            cities: [{
                cityTypes: "GG",
                citySource: "sdsf"
              },
              {
                cityTypes: "HH",
                citySource: "sdsgf"
              },
              {
                cityTypes: "II",
                citySource: "fgsdfgd"
              }
            ]
          }
        ]
        var cityTypes1 = 'BB';
        console.log(obj.reduce((total, cur) => 
            total||cur.cities.reduce((total, cur) => 
                total||cur.cityTypes === cityTypes1, false), 
            false))

        【讨论】:

          【解决方案4】:

          如果至少有一个城市具有所需的类型,则此简短方法返回 true:

          function findMyCode(code) {
             return obj.some((d) => {
                 return d.cities.some((city) => {
                     return city.cityTypes === code;
                 });
             });
          }
          

          或者你可以尝试使用lodash库。

          所以你可以使用它:

          var hasType = findMyCode('BB'); // returns true/false
          

          【讨论】:

            【解决方案5】:

            试试下面的代码。

            var result = false;
            for(let k=0; k<obj.length; k++){
                if(obj[k].cities){
                    let cityObj = obj[k].cities;
                    for(let city in cityObj){
                        if(cityObj[city].cityTypes == "BB"){
                            result = true;
                        }
                    }
                }
            }
            

            返回结果或打印控制台日志。

            【讨论】:

              【解决方案6】:
              var filtered = obj.filter((o) => {
                  var found=false;
                  for(let i=0;  i<o.cities.length; i++) {
                      let city = o.cities[i];
                      if(city.cityTypes === "BB"){
                          found=true;
                          break;
                      }
                  }
                  return found;
              })
              console.log(filtered)
              

              【讨论】:

                猜你喜欢
                • 2011-04-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-03-25
                • 2017-07-22
                • 1970-01-01
                • 2021-12-16
                相关资源
                最近更新 更多