Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Convert Array to Object with ordering change?

I have a array now want to convert into object with ordering change.

My Code:-

arr = [function() {}, new Object(), [], {}, NaN, Infinity, undefined, null, 0];

console.log({...arr});

but I want result like given below format, how can I achieve that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

OutPut: { function: 1, object: 4, number: 3, undefined: 1 }

Thanks for your efforts!

>Solution :

You just have to get the typeof each of the entries and sum it up in an object.

Here is an example:

const arr = [function() {}, new Object(), [], {}, NaN, Infinity, undefined, null, 0];

const obj = arr.map(e => typeof e).reduce((o,t) => ({...o, [t]: (o[t] ?? 0) + 1 }), {})
console.log(obj);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading