Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
February 2, 2024
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.