Home > Net >  Collect key/value pairs by key matching a separate arrays value
Collect key/value pairs by key matching a separate arrays value

Time:11-19

I am trying to get the values and keys in nested arrays by the key in "data" matching a comparison array's values

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
        }
    ]
]

Comparison array is

fields = ["APN", "Situs1", "Acreage", "ZONING", "ZonDescr", "ZonDescrip", "LAND_USE", "LU_Descrip"]

From here I want to return the title of each array and the corresponding key value pair that match values in "fields" like so

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"
        }
    ]
]

I have tried the following

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)
                }

But comes up undefined. Any help would be really appreciated. Thanks in advance!

CodePudding user response:

Take the entries of the object and filter them by whether the key is included in the comparison array. Then you can turn it back into an object with Object.fromEntries.

This will transform the objects as needed, which can be done with .map, but you also need to remove the subarrays that have only an empty object remaining, with .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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related