目前我有以下对象数组
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"
}
}
}
实现这一目标的正确方法是什么?
您可以在另一个.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'))
);
由于以下几个原因,您现有的代码不起作用:
- 您需要执行
let city of cityObj
,因为您正在内部循环中访问city
的属性作为city.cityTypes
,因此使用of
将为您提供变量city
中的每个对象。 - 您需要实际检查是否找到匹配项,使用
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);
使用双重归约应该有效
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))
如果至少有一个城市具有必需的类型,则此简短方法返回 true:
function findMyCode(code) {
return obj.some((d) => {
return d.cities.some((city) => {
return city.cityTypes === code;
});
});
}
或者你可以尝试使用洛达什库。
因此,您可以使用它:
var hasType = findMyCode('BB'); // returns true/false
试试下面的代码。
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;
}
}
}
}
返回结果或打印控制台日志。
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)