I have an array of objects [{...}, {...}] with 2 objects inside. I would like to filter them with values from an array using JavaScript. The inital object could be any length from 1 to n. I think I need to filter on the key from the json data in a loop using .includes()
This is a mess but I think this needs to be inside another loop for the length of jsonData
for (i=0; i<finalArray.length; i ){
jsonData= Object.fromEntries(Object.entries(jsonData).filter(([key, value]) => key.includes(finalArray[i])) )
}
0: { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053", "hh_50k_100k_201612": 71, "hh_50k_100k_201706": 86, "hh_50k_100k_201712": 60, "hh_50k_100k_201806": 37, "hh_50k_100k_201812": 49, "hh_50k_100k_201906": 35, "hh_50k_100k_201912": 38, "hh_50k_100k_202006": 46, "hh_50k_100k_202012": 58, "hh_100k_250k_201612": 120, "hh_100k_250k_201706": 121, "hh_100k_250k_201712": 153, "hh_100k_250k_201806": 126, "hh_100k_250k_201812": 126, "hh_100k_250k_201906": 125, "hh_100k_250k_201912": 120, "hh_100k_250k_202006": 99, "hh_100k_250k_202012": 84}
1: { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053","hh_50k_100k_201612": 20, "hh_50k_100k_201706": 33, "hh_50k_100k_201712": 22, "hh_50k_100k_201806": 41, "hh_50k_100k_201812": 52, "hh_50k_100k_201906": 45, "hh_50k_100k_201912": 40, "hh_50k_100k_202006": 41, "hh_50k_100k_202012": 50, "hh_100k_250k_201612": 99, "hh_100k_250k_201706": 108, "hh_100k_250k_201712": 130, "hh_100k_250k_201806": 84, "hh_100k_250k_201812": 90, "hh_100k_250k_201906": 97, "hh_100k_250k_201912": 89, "hh_100k_250k_202006": 95, "hh_100k_250k_202012": 87}
The array I would like to use as a filter
FilterArray = [ "HH_50K_100K_201612", "HH_50K_100K_201706", "HH_100K_250K_201612", "HH_100K_250K_201706" ]
Final output would be best if it modifies the original.
jsonData=
0: {"hh_50k_100k_201612": 71, "hh_50k_100k_201706": 86, "hh_100k_250k_201612": 120, "hh_100k_250k_201706": 121}
1: {"hh_50k_100k_201612": 20, "hh_50k_100k_201706": 33, "hh_100k_250k_201612": 99, "hh_100k_250k_201706": 108}
CodePudding user response:
Use map()
to loop over the array of objects and create a new array with the result of filtering the object properties.
You should be using FilterArray.includes()
, not key.includes()
.
const finalArray = [ { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053", "hh_50k_100k_201612": 71, "hh_50k_100k_201706": 86, "hh_50k_100k_201712": 60, "hh_50k_100k_201806": 37, "hh_50k_100k_201812": 49, "hh_50k_100k_201906": 35, "hh_50k_100k_201912": 38, "hh_50k_100k_202006": 46, "hh_50k_100k_202012": 58, "hh_100k_250k_201612": 120, "hh_100k_250k_201706": 121, "hh_100k_250k_201712": 153, "hh_100k_250k_201806": 126, "hh_100k_250k_201812": 126, "hh_100k_250k_201906": 125, "hh_100k_250k_201912": 120, "hh_100k_250k_202006": 99, "hh_100k_250k_202012": 84}, { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053","hh_50k_100k_201612": 20, "hh_50k_100k_201706": 33, "hh_50k_100k_201712": 22, "hh_50k_100k_201806": 41, "hh_50k_100k_201812": 52, "hh_50k_100k_201906": 45, "hh_50k_100k_201912": 40, "hh_50k_100k_202006": 41, "hh_50k_100k_202012": 50, "hh_100k_250k_201612": 99, "hh_100k_250k_201706": 108, "hh_100k_250k_201712": 130, "hh_100k_250k_201806": 84, "hh_100k_250k_201812": 90, "hh_100k_250k_201906": 97, "hh_100k_250k_201912": 89, "hh_100k_250k_202006": 95, "hh_100k_250k_202012": 87} ],
filterArray = [ "HH_50K_100K_201612", "HH_50K_100K_201706", "HH_100K_250K_201612", "HH_100K_250K_201706" ];
const result = finalArray.map(jsonData =>
Object.fromEntries(Object.entries(jsonData).filter(([key, value]) => filterArray.includes(key.toUpperCase()))));
console.log(result);
CodePudding user response:
You can use array#map
with Object.fromEntries()
. Iterate over each object and map over key of filterArray
and generate the resultant object.
const data = [ { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053", "hh_50k_100k_201612": 71, "hh_50k_100k_201706": 86, "hh_50k_100k_201712": 60, "hh_50k_100k_201806": 37, "hh_50k_100k_201812": 49, "hh_50k_100k_201906": 35, "hh_50k_100k_201912": 38, "hh_50k_100k_202006": 46, "hh_50k_100k_202012": 58, "hh_100k_250k_201612": 120, "hh_100k_250k_201706": 121, "hh_100k_250k_201712": 153, "hh_100k_250k_201806": 126, "hh_100k_250k_201812": 126, "hh_100k_250k_201906": 125, "hh_100k_250k_201912": 120, "hh_100k_250k_202006": 99, "hh_100k_250k_202012": 84}, { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053","hh_50k_100k_201612": 20, "hh_50k_100k_201706": 33, "hh_50k_100k_201712": 22, "hh_50k_100k_201806": 41, "hh_50k_100k_201812": 52, "hh_50k_100k_201906": 45, "hh_50k_100k_201912": 40, "hh_50k_100k_202006": 41, "hh_50k_100k_202012": 50, "hh_100k_250k_201612": 99, "hh_100k_250k_201706": 108, "hh_100k_250k_201712": 130, "hh_100k_250k_201806": 84, "hh_100k_250k_201812": 90, "hh_100k_250k_201906": 97, "hh_100k_250k_201912": 89, "hh_100k_250k_202006": 95, "hh_100k_250k_202012": 87} ],
filterArray = [ "HH_50K_100K_201612", "HH_50K_100K_201706", "HH_100K_250K_201612", "HH_100K_250K_201706" ],
result = data.map(o => Object.fromEntries(filterArray.map(key => [key.toLowerCase(), o[key.toLowerCase()]])));
console.log(result);