I am trying convert an array: filterArrays([1, 2, true, true, false, '1', 'a', 3, {}, null, undefined])
following pattern
{
"number": [
1,
2,
3
],
"boolean": [
true,
true,
false
],
"string": [
"1",
"a"
],
"object": [
{},
null
],
"undefined": [
null
]
}
Here is my try: which is working as expected value, Is there a better way to solve this problem? Thanks;
const filterArrays = (values) => {
return values.reduce((acc, obj) => {
const key = typeof obj;
if(acc[key]) {
acc[key].push(obj)
}
else {
acc[key] = [obj]
}
return acc;
}, {});
}