【发布时间】: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