My code involves two maps: charamap and filterdata. The goal is to filter charamap so that it only displays data for items listed in filterdata. The expected result should be:
[LOG]: ["Football":1, "Golf":0]
However, the actual output is:
[LOG]: ["Football", "Golf"]
What could be causing this issue in my code?
const charamap = {
"Football": 1,
"Cricket": 3,
"Golf": 0,
"Swimming": 10,
};
const filterdata = {
"fields": [ "Football", "Golf"]
};
// flatten charamap
const aa = JSON.stringify(charamap);
const resultcharac = JSON.parse(aa);
const ss = Array.from(resultcharac);
//get fields in array
const bb = JSON.stringify(filterdata);
const resultfields = JSON.parse(bb);
const rr = resultfields.fields;
//filter based on the 2 arrays
const res = rr
.reduce((obj: any, key: any) => ({ ...obj, [key]: ss[key] }), {});
console.log(res);
//Updated to use .reduce instead of .filter