在子对象对象内部搜索-JavaScript

时间:2018-09-07 07:04:09

标签: javascript arrays loops object search

目前我有下面的对象数组

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

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

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"
        }
    }
}

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

6 个答案:

答案 0 :(得分: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'))
);

答案 1 :(得分:1)

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

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

break

答案 2 :(得分:0)

使用双倍减少应该可以

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))

答案 3 :(得分:0)

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)

答案 4 :(得分:0)

如果至少一个城市具有必需的类型,则此short方法返回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 :(得分:0)

尝试以下代码。

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;
            }
        }
    }
}

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