Monday, 5 March 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...