我尝试通过"data“中的键匹配比较数组的值来获取嵌套数组中的值和键
data =
[
[
"SB County Land Use Boundaries",
{
"FID": 815,
"Handle": "B0D",
"LAND_USE": "CITY",
"GEN_CLASS": "No Jurisdiction",
"GEN_TYPE": "No Jurisdiction",
"LU_Descrip": "Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",
"LU_UnPerAc": 0,
"Join_ID": 488,
"Shape__Area": 69469732.26171875,
"Shape__Length": 86576.24883405081
}
],
[
"SB County Zoning",
{
"FID": 1149,
"ET_ID": 491,
"Handle": "1922",
"ZONING": "CITY",
"GEN_CLASS": "No Jurisdiction",
"GEN_TYPE": "No Jurisdiction",
"ZonDescrip": "Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",
"ZonUnPerAc": 0,
"Acres": 11648,
"test_ID": 488,
"ET_X": 6044294.06156,
"ET_Y": 1975709.1194,
"Join_ID": 492,
"Shape__Area": 69469789.37890625,
"Shape__Length": 86576.11064503175
}
],
[
"Property Parcels",
{
"FID": 15444,
"APN": "037-400-008",
"LAYER": "Ground",
"Situs1": "825 STATE ST",
"Acreage": 0.18,
"AgPres": " ",
"Shape__Area": 1072.97265625,
"Shape__Length": 154.93547123003384
}
],
[
"SB City Zoning",
{
"OBJECTID": 58,
"ZONE": "C-2",
"PUD": "N",
"S_D_1": "N",
"S_D_2": "N",
"S_D_3": "N",
"C_X": "N",
"P_D": "N",
"S_H": "N",
"ACRES": 0.816,
"ZONE_OTHER": "C-2",
"Zone_1": "C-G",
"Zone_Descr": "Commercial General",
"Zone_old": "C-2",
"Zone_old_D": "Commercial",
"ZoneOther_": "C-2",
"ZoneOther": "C-G",
"ZONEDESG": " ",
"SP6": " ",
"Shape__Area": 14444769.935546875,
"Shape__Length": 39069.464156507274
}
],
[
"SB City Land Use Boundaries",
{
"OBJECTID": 293,
"LUCode_Concept": "C-MHDR",
"ORIG_FID": 3,
"Area": 232293606.192763,
"Acres": 37.03282927,
"LUDesign_Concept": "Commercial-Medium High Density Residential",
"Shape__Area": 1613146.2373046875,
"Shape__Length": 12139.290187575602
}
]
]比较数组为
fields = ["APN", "Situs1", "Acreage", "ZONING", "ZonDescr", "ZonDescrip", "LAND_USE", "LU_Descrip"]在这里,我想返回每个数组的标题和与“field”中的值相匹配的相应键值对,如下所示
results = [
[
"SB County Land Use Boundaries",
{
"LAND_USE": "CITY",
"LU_Descrip": "Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",
}
],
[
"SB County Zoning",
{
"ZONING": "CITY",
"ZonDescrip": "Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",
}
],
[
"Property Parcels",
{
"APN": "037-400-008",
"Situs1": "825 STATE ST",
"Acreage": 0.18
}
],
[
"SB City Zoning",
{
"Zone_Descr": "Commercial General"
}
]
]我尝试过以下几种方法
for(var i=0;i<data.length;i++ ) {
var results = data[i].filter((d: any) => d.fields.every((c: string) => fields.includes(c)));
console.log(results)
}但出现了未定义的。任何帮助都将不胜感激。提前感谢!
发布于 2021-11-15 01:39:08
获取对象的条目,并根据比较数组中是否包含键来过滤它们。然后,您可以使用Object.fromEntries将其转换回对象。
这将根据需要转换对象,这可以使用.map完成,但是您还需要使用.filter删除只剩下一个空对象的子数组。
const data=[["SB County Land Use Boundaries",{FID:815,Handle:"B0D",LAND_USE:"CITY",GEN_CLASS:"No Jurisdiction",GEN_TYPE:"No Jurisdiction",LU_Descrip:"Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",LU_UnPerAc:0,Join_ID:488,Shape__Area:69469732.26171875,Shape__Length:86576.24883405081}],["SB County Zoning",{FID:1149,ET_ID:491,Handle:"1922",ZONING:"CITY",GEN_CLASS:"No Jurisdiction",GEN_TYPE:"No Jurisdiction",ZonDescrip:"Incorporated City (No County Jurisdiction): Santa Barbara, Carpinteria, Lompoc, Buellton, Solvang, Santa Maria, Guadalupe, Goleta",ZonUnPerAc:0,Acres:11648,test_ID:488,ET_X:6044294.06156,ET_Y:1975709.1194,Join_ID:492,Shape__Area:69469789.37890625,Shape__Length:86576.11064503175}],["Property Parcels",{FID:15444,APN:"037-400-008",LAYER:"Ground",Situs1:"825 STATE ST",Acreage:.18,AgPres:" ",Shape__Area:1072.97265625,Shape__Length:154.93547123003384}],["SB City Zoning",{OBJECTID:58,ZONE:"C-2",PUD:"N",S_D_1:"N",S_D_2:"N",S_D_3:"N",C_X:"N",P_D:"N",S_H:"N",ACRES:.816,ZONE_OTHER:"C-2",Zone_1:"C-G",Zone_Descr:"Commercial General",Zone_old:"C-2",Zone_old_D:"Commercial",ZoneOther_:"C-2",ZoneOther:"C-G",ZONEDESG:" ",SP6:" ",Shape__Area:14444769.935546875,Shape__Length:39069.464156507274}],["SB City Land Use Boundaries",{OBJECTID:293,LUCode_Concept:"C-MHDR",ORIG_FID:3,Area:232293606.192763,Acres:37.03282927,LUDesign_Concept:"Commercial-Medium High Density Residential",Shape__Area:1613146.2373046875,Shape__Length:12139.290187575602}]],fields=["APN","Situs1","Acreage","ZONING","ZonDescr","ZonDescrip","LAND_USE","LU_Descrip"];
const output = data
.map(([name, obj]) => [name, Object.fromEntries(
Object.entries(obj)
.filter(([key]) => fields.includes(key))
)])
.filter(([, obj]) => Object.keys(obj).length);
console.log(output);
https://stackoverflow.com/questions/69968614
复制相似问题