const cards = [
{gaps: ["science", "education"]},
{gaps: ["weapon"]},
{gaps: ["figures", "character"]},
{gaps: ["figures", "education"]},
]
function filter({})
Please help, there is an array when calling filter () this should return 5 groups into the console
enter code hereducation"]}, {gaps: ["figures", "education"]} - educationUsing Array#reduce, iterate over cards while updating a Map where the key is the gap and the value is the list of objects having this gap in its gaps array.
In each iteration, you would iterate over the gaps using Array#forEach and update the Map:
const cards = [ {gaps: ["science", "education"]}, {gaps: ["weapon"]}, {gaps: ["figures", "character"]}, {gaps: ["figures", "education"]} ];
const gapsMap = cards.reduce((map, { gaps = [] }) => {
gaps.forEach(gap => map.set(gap, [...(map.get(gap) || []), {gaps: [...gaps]}]));
return map;
}, new Map);
console.log([...gapsMap]);
console.log(gapsMap.get("education"));