如何根据&#39; id&#39; 或&#39; subdivisions.id&#39; <过滤下面的对象数组? /强>
ps: id 是唯一的,你找不到object.id == object.subdivisions [n] .id
{
"id": "1",
"name": "South East",
"subdivisions": [
{
"id": "2",
"name": "Berkshire"
},
{
"id": "3",
"name": "Buckinghamshire"
},
{
"id": "4",
"name": "East Sussex"
},
{
"id": "5",
"name": "Hampshire"
},
{
"id": "6",
"name": "Isle of Wight"
},
{
"id": "7",
"name": "Kent"
},
{
"id": "8",
"name": "Oxfordshire"
},
{
"id": "9",
"name": "Surrey"
},
{
"id": "10",
"name": "West Sussex"
}
]
}
并得到一个像这样的简单对象:
{
"id": "10",
"name": "West Sussex"
}
修改
这是完整的数组:
[
{
"id": "1",
"name": "South East",
"subdivisions": [
{
"id": "2",
"name": "Berkshire",
"__typename": "Region"
},
{
"id": "3",
"name": "Buckinghamshire",
"__typename": "Region"
},
{
"id": "4",
"name": "East Sussex",
"__typename": "Region"
},
{
"id": "5",
"name": "Hampshire",
"__typename": "Region"
},
{
"id": "6",
"name": "Isle of Wight",
"__typename": "Region"
},
{
"id": "7",
"name": "Kent",
"__typename": "Region"
},
{
"id": "8",
"name": "Oxfordshire",
"__typename": "Region"
},
{
"id": "9",
"name": "Surrey",
"__typename": "Region"
},
{
"id": "10",
"name": "West Sussex",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "11",
"name": "London",
"subdivisions": [
{
"id": "12",
"name": "City of London",
"__typename": "Region"
},
{
"id": "13",
"name": "City of Westminster",
"__typename": "Region"
},
{
"id": "14",
"name": "Kensington and Chelsea",
"__typename": "Region"
},
{
"id": "15",
"name": "Hammersmith and Fulham",
"__typename": "Region"
},
{
"id": "16",
"name": "Wandsworth",
"__typename": "Region"
},
{
"id": "17",
"name": "Lambeth",
"__typename": "Region"
},
{
"id": "18",
"name": "Southwark",
"__typename": "Region"
},
{
"id": "19",
"name": "Tower Hamlets",
"__typename": "Region"
},
{
"id": "20",
"name": "Hackney",
"__typename": "Region"
},
{
"id": "21",
"name": "Islington",
"__typename": "Region"
},
{
"id": "22",
"name": "Camden",
"__typename": "Region"
},
{
"id": "23",
"name": "Brent",
"__typename": "Region"
},
{
"id": "24",
"name": "Ealing",
"__typename": "Region"
},
{
"id": "25",
"name": "Hounslow",
"__typename": "Region"
},
{
"id": "26",
"name": "Richmond",
"__typename": "Region"
},
{
"id": "27",
"name": "Kingston",
"__typename": "Region"
},
{
"id": "28",
"name": "Merton",
"__typename": "Region"
},
{
"id": "29",
"name": "Sutton",
"__typename": "Region"
},
{
"id": "30",
"name": "Croydon",
"__typename": "Region"
},
{
"id": "31",
"name": "Bromley",
"__typename": "Region"
},
{
"id": "32",
"name": "Lewisham",
"__typename": "Region"
},
{
"id": "33",
"name": "Greenwich",
"__typename": "Region"
},
{
"id": "34",
"name": "Bexley",
"__typename": "Region"
},
{
"id": "35",
"name": "Havering",
"__typename": "Region"
},
{
"id": "36",
"name": "Barking and Dagenham",
"__typename": "Region"
},
{
"id": "37",
"name": "Redbridge",
"__typename": "Region"
},
{
"id": "38",
"name": "Newham",
"__typename": "Region"
},
{
"id": "39",
"name": "Waltham Forest",
"__typename": "Region"
},
{
"id": "40",
"name": "Haringey",
"__typename": "Region"
},
{
"id": "41",
"name": "Enfield",
"__typename": "Region"
},
{
"id": "42",
"name": "Barnet",
"__typename": "Region"
},
{
"id": "43",
"name": "Harrow",
"__typename": "Region"
},
{
"id": "44",
"name": "Hillingdon",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "45",
"name": "North West England",
"subdivisions": [
{
"id": "46",
"name": "Cheshire",
"__typename": "Region"
},
{
"id": "47",
"name": "Cumbria",
"__typename": "Region"
},
{
"id": "48",
"name": "Greater Manchester",
"__typename": "Region"
},
{
"id": "49",
"name": "Lancashire",
"__typename": "Region"
},
{
"id": "50",
"name": "Merseyside",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "51",
"name": "East of England",
"subdivisions": [
{
"id": "52",
"name": "East Anglia",
"__typename": "Region"
},
{
"id": "53",
"name": "Bedfordshire and Hertfordshire",
"__typename": "Region"
},
{
"id": "54",
"name": "Essex",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "55",
"name": "West Midlands",
"subdivisions": [
{
"id": "56",
"name": "Herefordshire, Worcestershire and Warwickshire",
"__typename": "Region"
},
{
"id": "57",
"name": "Shropshire and Staffordshire",
"__typename": "Region"
},
{
"id": "58",
"name": "West Midlands",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "59",
"name": "South West England",
"subdivisions": [
{
"id": "60",
"name": "Gloucestershire, Wiltshire and Bristol/Bath area",
"__typename": "Region"
},
{
"id": "61",
"name": "Dorset and Somerset",
"__typename": "Region"
},
{
"id": "62",
"name": "Cornwall and Isles of Scilly",
"__typename": "Region"
},
{
"id": "63",
"name": "Devon",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "64",
"name": "Yorkshire and the Humber",
"subdivisions": [
{
"id": "65",
"name": "East Riding and North Lincolnshire",
"__typename": "Region"
},
{
"id": "66",
"name": "North Yorkshire",
"__typename": "Region"
},
{
"id": "67",
"name": "South Yorkshire",
"__typename": "Region"
},
{
"id": "68",
"name": "West Yorkshire",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "69",
"name": "East Midlands",
"subdivisions": [
{
"id": "70",
"name": "Derbyshire and Nottinghamshire",
"__typename": "Region"
},
{
"id": "71",
"name": "Leicestershire, Rutland and Northamptonshire",
"__typename": "Region"
},
{
"id": "72",
"name": "Lincolnshire",
"__typename": "Region"
}
],
"__typename": "Region"
},
{
"id": "73",
"name": "North East England",
"subdivisions": [
{
"id": "74",
"name": "Tees Valley and County Durham",
"__typename": "Region"
},
{
"id": "75",
"name": "Northumberland and Tyne and Wear",
"__typename": "Region"
}
],
"__typename": "Region"
}
]
答案 0 :(得分:4)
您可以使用Array#find
。
let result = object.subdivisions.find(sub => sub.id === id);
对于在subdivisions
之外或在function find(data, id) {
var result = data.find(o => o.id === id);
if (!result) {
data.some(o => result = o.subdivisions.find(s => s.id === id));
}
return result;
}
var data = [{ id: "1", name: "South East", subdivisions: [{ id: "2", name: "Berkshire", __typename: "Region" }, { id: "3", name: "Buckinghamshire", __typename: "Region" }, { id: "4", name: "East Sussex", __typename: "Region" }, { id: "5", name: "Hampshire", __typename: "Region" }, { id: "6", name: "Isle of Wight", __typename: "Region" }, { id: "7", name: "Kent", __typename: "Region" }, { id: "8", name: "Oxfordshire", __typename: "Region" }, { id: "9", name: "Surrey", __typename: "Region" }, { id: "10", name: "West Sussex", __typename: "Region" }], __typename: "Region" }, { id: "11", name: "London", subdivisions: [{ id: "12", name: "City of London", __typename: "Region" }, { id: "13", name: "City of Westminster", __typename: "Region" }, { id: "14", name: "Kensington and Chelsea", __typename: "Region" }, { id: "15", name: "Hammersmith and Fulham", __typename: "Region" }, { id: "16", name: "Wandsworth", __typename: "Region" }, { id: "17", name: "Lambeth", __typename: "Region" }, { id: "18", name: "Southwark", __typename: "Region" }, { id: "19", name: "Tower Hamlets", __typename: "Region" }, { id: "20", name: "Hackney", __typename: "Region" }, { id: "21", name: "Islington", __typename: "Region" }, { id: "22", name: "Camden", __typename: "Region" }, { id: "23", name: "Brent", __typename: "Region" }, { id: "24", name: "Ealing", __typename: "Region" }, { id: "25", name: "Hounslow", __typename: "Region" }, { id: "26", name: "Richmond", __typename: "Region" }, { id: "27", name: "Kingston", __typename: "Region" }, { id: "28", name: "Merton", __typename: "Region" }, { id: "29", name: "Sutton", __typename: "Region" }, { id: "30", name: "Croydon", __typename: "Region" }, { id: "31", name: "Bromley", __typename: "Region" }, { id: "32", name: "Lewisham", __typename: "Region" }, { id: "33", name: "Greenwich", __typename: "Region" }, { id: "34", name: "Bexley", __typename: "Region" }, { id: "35", name: "Havering", __typename: "Region" }, { id: "36", name: "Barking and Dagenham", __typename: "Region" }, { id: "37", name: "Redbridge", __typename: "Region" }, { id: "38", name: "Newham", __typename: "Region" }, { id: "39", name: "Waltham Forest", __typename: "Region" }, { id: "40", name: "Haringey", __typename: "Region" }, { id: "41", name: "Enfield", __typename: "Region" }, { id: "42", name: "Barnet", __typename: "Region" }, { id: "43", name: "Harrow", __typename: "Region" }, { id: "44", name: "Hillingdon", __typename: "Region" }], __typename: "Region" }, { id: "45", name: "North West England", subdivisions: [{ id: "46", name: "Cheshire", __typename: "Region" }, { id: "47", name: "Cumbria", __typename: "Region" }, { id: "48", name: "Greater Manchester", __typename: "Region" }, { id: "49", name: "Lancashire", __typename: "Region" }, { id: "50", name: "Merseyside", __typename: "Region" }], __typename: "Region" }, { id: "51", name: "East of England", subdivisions: [{ id: "52", name: "East Anglia", __typename: "Region" }, { id: "53", name: "Bedfordshire and Hertfordshire", __typename: "Region" }, { id: "54", name: "Essex", __typename: "Region" }], __typename: "Region" }, { id: "55", name: "West Midlands", subdivisions: [{ id: "56", name: "Herefordshire, Worcestershire and Warwickshire", __typename: "Region" }, { id: "57", name: "Shropshire and Staffordshire", __typename: "Region" }, { id: "58", name: "West Midlands", __typename: "Region" }], __typename: "Region" }, { id: "59", name: "South West England", subdivisions: [{ id: "60", name: "Gloucestershire, Wiltshire and Bristol/Bath area", __typename: "Region" }, { id: "61", name: "Dorset and Somerset", __typename: "Region" }, { id: "62", name: "Cornwall and Isles of Scilly", __typename: "Region" }, { id: "63", name: "Devon", __typename: "Region" }], __typename: "Region" }, { id: "64", name: "Yorkshire and the Humber", subdivisions: [{ id: "65", name: "East Riding and North Lincolnshire", __typename: "Region" }, { id: "66", name: "North Yorkshire", __typename: "Region" }, { id: "67", name: "South Yorkshire", __typename: "Region" }, { id: "68", name: "West Yorkshire", __typename: "Region" }], __typename: "Region" }, { id: "69", name: "East Midlands", subdivisions: [{ id: "70", name: "Derbyshire and Nottinghamshire", __typename: "Region" }, { id: "71", name: "Leicestershire, Rutland and Northamptonshire", __typename: "Region" }, { id: "72", name: "Lincolnshire", __typename: "Region" }], __typename: "Region" }, { id: "73", name: "North East England", subdivisions: [{ id: "74", name: "Tees Valley and County Durham", __typename: "Region" }, { id: "75", name: "Northumberland and Tyne and Wear", __typename: "Region" }], __typename: "Region" }];
console.log(find(data, 'foo')); // undefined
console.log(find(data, '66')); // item from subdivisions
console.log(find(data, '69')); // item from outer array
内的搜索,您可以使用以下
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alt + Enter