Monday, March 5, 2018

javascript - Make sure variable is array

I want to run map() on a variable that might either be a single object or an array. However, I can't do this if the variable is an object as that throws an error.


I tried using the spread operator to make sure the variable turns into an array but apparently that doesn't work either:


// items can either be an object or an array of objects
renderedItems = [...items]
.filter(item => !!item)
.map((item, index) => _this.renderItem(item, index));

How can I make sure the variable I'm doing a filter() and map() on is an array? I heard Array.isArray isn't fully supported yet.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...