Based on the question example, we can apply filter conditionally

Let’s say we want to skip the filter for the all type condition but what to do filter for all the other types. We can use if condition or ternary operator to check is and return result based on that

let assets = [
   { id: 1, type: 'image', url: 'image.jpg' },
   { id: 2, type: 'video', url: 'video.mp4' }
]

let currentOption = 'image'

function getFilteredResult() {
   if (currentOption === 'all') {
       return assets
   } else {
       return assets.filter(a => a.type === currentOption)
   }
}
getFilteredResult();

Not if you look at the code above. We have an assets array with all the different types of data for image and video.

currentOption is the current data we want to filter from assets. And if currentOption is `all` we will return all data from array. We have create one getFilteredResult function which will do it conditionally.

Support On Demand!

JavaScript