我有以下代码用于使用另一个嵌套数组中的值过滤一个嵌套数组,但我正在寻找更快的方法。
const length = this.addedStateItems.length;
for (let i = 0; i < length; i++) {
if (this.addedStateItems[i].cities.length > 0) {
const tempCity: Array<BWCity> = [];
for (let k = 0; k < addedStates.length; k++) {
for (let l = 0; l < addedStates[k].Cities.length; l++) {
const city = this.addedStateItems[i].cities.find(item =>
item.name.toUpperCase().split(' ').join('_') === addedStates[k].Cities[l].CityName
);
if (city) {
tempCity.push(city);
}
}
}
this.addedStateItems[i].cities = tempCity;
}
}
JSON示例如下:
this.addedStateItems = [
{
"isSelected": false,
"cities": [],
"name": "Alabama",
"code": "AL",
"sequence": 2,
"access": 2,
"id": 2
},
{
"isSelected": false,
"cities": [],
"name": "Alaska",
"code": "AK",
"sequence": 3,
"access": 3,
"id": 3
},
{
"isSelected": false,
"cities": [],
"name": "Arizona",
"code": "AZ",
"sequence": 4,
"access": 4,
"id": 4
},
{
"isSelected": false,
"cities": [],
"name": "Arkansas",
"code": "AR",
"sequence": 5,
"access": 5,
"id": 5
},
{
"isSelected": false,
"cities": [],
"name": "California",
"code": "CA",
"sequence": 6,
"access": 6,
"id": 6
},
{
"isSelected": false,
"cities": [],
"name": "Colorado",
"code": "CO",
"sequence": 7,
"access": 7,
"id": 7
},
{
"isSelected": false,
"cities": [],
"name": "Connecticut",
"code": "CT",
"sequence": 8,
"access": 8,
"id": 8
},
{
"isSelected": false,
"cities": [],
"name": "Delaware",
"code": "DE",
"sequence": 9,
"access": 9,
"id": 9
},
{
"isSelected": false,
"cities": [],
"name": "District of Columbia",
"code": "DC",
"sequence": 10,
"access": 10,
"id": 10
},
{
"isSelected": false,
"cities": [],
"name": "Florida",
"code": "FL",
"sequence": 11,
"access": 11,
"id": 11
},
{
"isSelected": false,
"cities": [],
"name": "Georgia",
"code": "GA",
"sequence": 12,
"access": 12,
"id": 12
},
{
"isSelected": false,
"cities": [],
"name": "Hawaii",
"code": "HI",
"sequence": 13,
"access": 13,
"id": 13
},
{
"isSelected": false,
"cities": [],
"name": "Idaho",
"code": "ID",
"sequence": 14,
"access": 14,
"id": 14
},
{
"isSelected": false,
"cities": [],
"name": "Illinois",
"code": "IL",
"sequence": 15,
"access": 15,
"id": 15
},
{
"isSelected": false,
"cities": [],
"name": "Indiana",
"code": "IN",
"sequence": 16,
"access": 16,
"id": 16
},
{
"isSelected": false,
"cities": [],
"name": "Iowa",
"code": "IA",
"sequence": 17,
"access": 17,
"id": 17
},
{
"isSelected": false,
"cities": [],
"name": "Kansas",
"code": "KS",
"sequence": 18,
"access": 18,
"id": 18
},
{
"isSelected": false,
"cities": [],
"name": "Kentucky",
"code": "KY",
"sequence": 19,
"access": 19,
"id": 19
},
{
"isSelected": false,
"cities": [],
"name": "Louisiana",
"code": "LA",
"sequence": 20,
"access": 20,
"id": 20
},
{
"isSelected": false,
"cities": [],
"name": "Maine",
"code": "ME",
"sequence": 21,
"access": 21,
"id": 21
},
{
"isSelected": false,
"cities": [],
"name": "Maryland",
"code": "MD",
"sequence": 22,
"access": 22,
"id": 22
},
{
"isSelected": false,
"cities": [],
"name": "Massachusetts",
"code": "MA",
"sequence": 23,
"access": 23,
"id": 23
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 9
}
],
"name": "MI Generic",
"code": "MIG",
"id": 1
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 10
}
],
"name": "Port Huron",
"code": "POH",
"id": 9
},
{
"isSelected": false,
"citySequences": [],
"name": "Hudson",
"code": "HUD",
"id": 31
}
],
"name": "Michigan",
"code": "MI",
"sequence": 24,
"access": 24,
"id": 24
},
{
"isSelected": false,
"cities": [],
"name": "Minnesota",
"code": "MN",
"sequence": 25,
"access": 25,
"id": 25
},
{
"isSelected": false,
"cities": [],
"name": "Mississippi",
"code": "MS",
"sequence": 26,
"access": 26,
"id": 26
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
}
],
"name": "Kansas City",
"code": "KCM",
"id": 29
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
}
],
"name": "St Louis",
"code": "STL",
"id": 30
}
],
"name": "Missouri",
"code": "MO",
"sequence": 27,
"access": 27,
"id": 27
},
{
"isSelected": false,
"cities": [],
"name": "Montana",
"code": "MT",
"sequence": 28,
"access": 28,
"id": 28
},
{
"isSelected": false,
"cities": [],
"name": "Nebraska",
"code": "NE",
"sequence": 29,
"access": 29,
"id": 29
},
{
"isSelected": false,
"cities": [],
"name": "New Hampshire",
"code": "NH",
"sequence": 31,
"access": 31,
"id": 31
},
{
"isSelected": false,
"cities": [],
"name": "New Jersey",
"code": "NJ",
"sequence": 32,
"access": 32,
"id": 32
},
{
"isSelected": false,
"cities": [],
"name": "New Mexico",
"code": "NM",
"sequence": 33,
"access": 33,
"id": 33
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "New York City",
"code": "NYC",
"id": 11
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
}
],
"name": "Yonkers",
"code": "NYY",
"id": 12
}
],
"name": "New York",
"code": "NY",
"sequence": 34,
"access": 34,
"id": 34
},
{
"isSelected": false,
"cities": [],
"name": "North Carolina",
"code": "NC",
"sequence": 35,
"access": 35,
"id": 36
},
{
"isSelected": false,
"cities": [],
"name": "North Dakota",
"code": "ND",
"sequence": 36,
"access": 36,
"id": 37
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 12
},
{
"systemId": 3,
"sequence": 3
},
{
"systemId": 4,
"sequence": 3
},
{
"systemId": 5,
"sequence": 3
}
],
"name": "OH Generic",
"code": "OHG",
"id": 13
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "OH RITA",
"code": "OHR",
"id": 14
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 3,
"sequence": 2
},
{
"systemId": 4,
"sequence": 2
},
{
"systemId": 5,
"sequence": 2
}
],
"name": "OH CCA",
"code": "CCA",
"id": 15
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 3
}
],
"name": "Akron",
"code": "AKR",
"id": 16
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 4
}
],
"name": "Cincinnati",
"code": "CIN",
"id": 17
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 6
}
],
"name": "Columbus",
"code": "COL",
"id": 19
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 7
}
],
"name": "Toledo",
"code": "TOL",
"id": 20
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 8
}
],
"name": "Springfield",
"code": "SPR",
"id": 21
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 9
}
],
"name": "Wyoming",
"code": "WYO",
"id": 22
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 10
}
],
"name": "Indian_Hill",
"code": "IHL",
"id": 23
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 11
}
],
"name": "Mansfield",
"code": "MAN",
"id": 24
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 12
}
],
"name": "Crestline",
"code": "CRE",
"id": 25
}
],
"name": "Ohio",
"code": "OH",
"sequence": 37,
"access": 37,
"id": 38
},
{
"isSelected": false,
"cities": [],
"name": "Oklahoma",
"code": "OK",
"sequence": 38,
"access": 38,
"id": 39
},
{
"isSelected": false,
"cities": [],
"name": "Oregon",
"code": "OR",
"sequence": 39,
"access": 39,
"id": 40
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
}
],
"name": "PA Generic",
"code": "PAG",
"id": 26
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "Philadelphia",
"code": "PHI",
"id": 27
}
],
"name": "Pennsylvania",
"code": "PA",
"sequence": 40,
"access": 40,
"id": 41
},
{
"isSelected": false,
"cities": [],
"name": "Rhode Island",
"code": "RI",
"sequence": 41,
"access": 41,
"id": 42
},
{
"isSelected": false,
"cities": [],
"name": "South Carolina",
"code": "SC",
"sequence": 42,
"access": 42,
"id": 43
},
{
"isSelected": false,
"cities": [],
"name": "Tennessee",
"code": "TN",
"sequence": 44,
"access": 44,
"id": 45
},
{
"isSelected": false,
"cities": [],
"name": "Texas",
"code": "TX",
"sequence": 45,
"access": 45,
"id": 46
},
{
"isSelected": false,
"cities": [],
"name": "Utah",
"code": "UT",
"sequence": 46,
"access": 46,
"id": 47
},
{
"isSelected": false,
"cities": [],
"name": "Vermont",
"code": "VT",
"sequence": 47,
"access": 47,
"id": 48
},
{
"isSelected": false,
"cities": [],
"name": "Virginia",
"code": "VA",
"sequence": 48,
"access": 48,
"id": 49
},
{
"isSelected": false,
"cities": [],
"name": "West Virginia",
"code": "WV",
"sequence": 50,
"access": 50,
"id": 51
},
{
"isSelected": false,
"cities": [],
"name": "Wisconsin",
"code": "WI",
"sequence": 51,
"access": 51,
"id": 52
}
]
第二个JSON:
addedStates = [
{
"StateName": "DISTRICT_OF_COLUMBIA",
"Cities": []
},
{
"StateName": "COLORADO",
"Cities": []
},
{
"StateName": "ALABAMA",
"Cities": []
},
{
"StateName": "OHIO",
"Cities": [
{
"CityName": "AKRON"
},
{
"CityName": "CINCINNATI"
}
]
},
{
"StateName": "NEW_YORK",
"Cities": [
{
"CityName": "YONKERS"
}
]
}
]
使用第二个JSON过滤第一个JSON后,我希望这样:
result = [
{
"isSelected": false,
"cities": [],
"name": "Alabama",
"code": "AL",
"sequence": 2,
"access": 2,
"id": 2
},
{
"isSelected": false,
"cities": [],
"name": "Colorado",
"code": "CO",
"sequence": 7,
"access": 7,
"id": 7
},
{
"isSelected": false,
"cities": [],
"name": "District of Columbia",
"code": "DC",
"sequence": 10,
"access": 10,
"id": 10
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
}
],
"name": "Yonkers",
"code": "NYY",
"id": 12
}
],
"name": "New York",
"code": "NY",
"sequence": 34,
"access": 34,
"id": 34
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 3
}
],
"name": "Akron",
"code": "AKR",
"id": 16
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 4
}
],
"name": "Cincinnati",
"code": "CIN",
"id": 17
}
],
"name": "Ohio",
"code": "OH",
"sequence": 37,
"access": 37,
"id": 38
}
]