JavaScript makes it easy to streamline conditions

Can someone help me simplify this repetitive condition?

if (this.get('fileUrl')) {
      const isUnsplash = this.get('fileContainer.asset_kind') === 'UnsplashAsset';
      return Asset.create({
          url: this.get('fileUrl'),
          mime_type: this.get('fileContainer.mime_type'),
          isUnsplashObject: isUnsplash
        });
    }

Answer №1

To update the isUnsplashObject attribute and return a single Asset.create({...}), you can follow this example:

if (this.get('fileUrl')) {
  return Asset.create({
    url: this.get('fileUrl'),
    mime_type: this.get('fileContainer.mime_type'),
    isUnsplashObject: (this.get('fileContainer.asset_kind') === 'UnsplashAsset')
  });
}

This is similar to the following approach:

if (this.get('fileContainer.asset_kind') === 'UnsplashAsset') {
  isUnsplashObject = true
} else {
  isUnsplashObject = false
}

Alternatively, you can use a ternary operator like this:

isUnsplashObject = (this.get('fileContainer.asset_kind') === 'UnsplashAsset') ? true : false;

Or simply return the equality check result as it already yields a boolean value:

isUnsplashObject = (this.get('fileContainer.asset_kind') === 'UnsplashAsset')

Answer №2

Make the conditional inside the isUnsplashObject to only change when necessary.

if (this.get('fileUrl')) {
    return Asset.create({
        url: this.get('fileUrl'),
        mime_type: this.get('fileContainer.mime_type'),
        isUnsplashObject: this.get('fileContainer.asset_kind') === 'UnsplashAsset'
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my YouTube code to malfunction with certain playlists?

Check out the first jsfiddle playlist here The Alum Songs Playlist is working perfectly, but unfortunately, the same code is not functioning for another playlist ID. View the second jsfiddle playlist here The Medical Animated Playlist is currently not w ...

R: Setting data values in motion

I have been collecting data from participants in a research project and need to assign scores based on their responses. A correct response is given a value of 1, while an incorrect response gets a score of 0. For example, let's say Q1 represents the ...

Passing Props in Material-UI v5xx with ReactJS: A Beginner's Guide

Struggling with the fact that useStyle() isn't functioning properly in my material-UI v5xx version, I found myself at a loss on how to pass props in this updated edition. In Material-UI v4, it was as simple as: const Navbar = () => { const [open ...

What is the best way to position a single element in React using the Grid Component without causing any overlap?

I am struggling with positioning 3 components on my react page: PageHeader, SideMenu & FeatureList (which consists of Display Cards). Here is the code for each component: App.js // App.js code here... PageHeader.js // PageHeader.js code here... SideMenu ...

Ways to include additional parameters in jQuery ajax success and error callbacks

When working with a jQuery AJAX success/error function similar to the following: success: function (data, textStatus, jqXHR) { } error: function (jqxr, errorCode, errorThrown) { } I am wondering if there is a method where I can pass an array of valu ...

CSS and Javascript functioning correctly within internal server, but encountering issues when accessed externally

I am in the process of creating a website for a friend. The goal is to have a flashy animated style website that functions well on IOS and allows him to make changes easily. To achieve this, I am utilizing JQuery, my own javascript, and multiple css files. ...

Hiding the keypad on an Android device in an Ionic app when user input is detected

I am currently utilizing the syncfusion ej2 Calendar plugin for a datepicker, but I am only using options such as selecting ranges like today, 1 month, or last 7 days from the plugin itself. The plugin provides dropdown options when the calendar is trigger ...

The process of creating a mind map with blank spaces included

I am working on creating a mapping system that links companies with their logos. The goal is to display these logos on a google-map. While completing the company-logo association map, I noticed that some vessel names contain white spaces, causing a compil ...

Guide to accessing JSON APIs by utilizing a link within another API that contains JSON data linking to yet another JSON file

Searching for a Solution to Fetch Data from a Specific URL and Display it on a New Screen I have successfully called the API using the following link: - Completed After receiving JSON data, I was able to populate my DataTable. - Completed Upon clicking a ...

Executing a child component function once the parent component data is loaded in Angular 5

In my project, I have a parent component called program-page.component where I am invoking a function to fetch some data. ngOnInit() { this.getProgress(); } getFirstProgramItem() { this._contentfulService.getProgramItem(4, 1) .then((programItem) = ...

Is it considered poor form to use res.locals in Node.js with Express?

Is it considered bad practice to use res.locals as shown in this code example? Are there potential issues that could arise from using it in this manner? app.use((req, res, next) => { const session = req.cookies.session if (session) { db. ...

Convert HTML table data to JSON format and customize cell values

Hey there, I have a HTML table that looks like this: <table id="people" border="1"> <thead> <tr> <th>Name</th> <th>Places</th> <th>Grade</th> </tr> & ...

The URL provided for the Ajax HTTP request is not accurate

Consider the following JavaScript code: <script type="text/javascript" charset="utf-8> function goForLogin() { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","/account/login",true); xmlhttp.s ...

Creating interactive navigation bar effects with scroll and click functionality using jQuery

Is there a way to make the navigation highlight when a user clicks on it and also scrolls to the corresponding section? Currently, I am facing an issue where only the third navigation event highlights, most likely because when navigating to the fourth or f ...

Angular does not alter the focus when a new component is loaded

Currently, I am working on resolving an accessibility issue with a screen reader in an Angular2 web application. When componentA (code shown below as Before) is loaded in Chrome, the entire browser window gains focus and the screen reader narrator announce ...

How to upload a file with JavaScript without using Ajax technology

Is it possible to upload a file using the input tag with type='file' and save it in my project folder without using ajax? Can JavaScript help me achieve this task? Below is the code snippet I am currently working on: function func3() { var ...

Is there a way to align the Material UI mobile menu to the left edge of the screen?

Need help with this UI issue: Currently working on designing a mobile web app using React and Material UI. I'm struggling to get the sidebar menu to start from the left edge of the screen, as it seems to have an unwanted margin preventing it. Any sug ...

The Discord.js .cleanContent attribute does not show up when using Object.keys() and cannot be logged

My Discord.js Message object should contain a property called .cleanContent, according to the documentation, and it should be a string. console.log(message.cleanContent) works correctly, but console.log(message) does not display the cleanContent propert ...

Tips for uploading a file with fetch

While I know this question has been asked before, none of the solutions seem to be working for me. Initially, I attempted to resolve the issue using axios, but it appears that there is a bug preventing me from utilizing it for file uploads. Therefore, I am ...

The issue of Angular UI Bootstrap buttons not updating persists even after the removal of an

My Radio-bottoms are powered by an Array for a Multi-Choice answer setup. <div ng-repeat="option in options"> <div> <button type="button" style="min-width: 100px" class="btn btn-default" ng-model="question.answer" btn-radio="' ...