Javascript Problems — Select only object from array

The goal of this exercise is to take an array of items: objects, arrays, numbers, strings and null, and return only true objects.  Both null and arrays are objects in Javascript, so I have to test for those specifically.  First I used the reduce method.  On line 7, I test if an item is null or an array and exclude it from the accumulator.  Then on line 11, I test to see if it is an object (that is not null or an array) and add it to the accumulator.  On line 17 I skip all other types (number and string) by returning the accumulator without adding the item.

Next, I doing it using the filter() method, which makes the code much more streamlined.  I do all the testing on line 5 to make sure the item is an object, but is not null and not an array.


Leave a Reply

Your email address will not be published.