首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在子对象数组中搜索- JavaScript

在子对象数组中搜索- JavaScript
EN

Stack Overflow用户
提问于 2018-09-07 07:04:09
回答 6查看 60关注 0票数 1

目前,我有以下的数组对象

代码语言:javascript
复制
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"
            }
        ]
    }
]

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

假设,我需要搜索cityTypes = BB

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

如果BB未预置,则返回false

这就是我尝试过的,但似乎行不通。

代码语言:javascript
复制
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"
        }
    }
}

什么是正确的方法来实现这一点?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2018-09-07 07:07:08

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

代码语言:javascript
复制
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'))
);

票数 1
EN

Stack Overflow用户

发布于 2018-09-07 07:07:20

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

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

代码语言:javascript
复制
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);

票数 1
EN

Stack Overflow用户

发布于 2018-09-07 07:13:48

使用双减缩应该有效

代码语言:javascript
复制
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))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52217029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档