Categories
TypeScript Answers

How to instantiate, initialize and populate an array in TypeScript?

Sometimes, we want to instantiate, initialize and populate an array in TypeScript.

In this article, we’ll look at how to instantiate, initialize and populate an array in TypeScript.

How to instantiate, initialize and populate an array in TypeScript?

To instantiate, initialize and populate an array in TypeScript, we can make a new class.

For instance, we write

class Bar {
  constructor(public length: number) {}
}

const bars = [new Bar(1)];

to define the Bar class that takes the length parameter in the constructor.

And it’ll assign the length value to the length instance property.

Then we define the bars array and assign it an array that has a Bar instance.

Conclusion

To instantiate, initialize and populate an array in TypeScript, we can make a new class.

Categories
JavaScript Answers

How to sort or order keys in JavaScript objects?

Sometimes, we want to sort or order keys in JavaScript objects.

In this article, we’ll look at how to sort or order keys in JavaScript objects.

How to sort or order keys in JavaScript objects?

To sort or order keys in JavaScript objects, we can use the Object.keys method and the array sort and reduce methods.

For instance, we write

const notSorted = { b: false, a: true };

const sorted = Object.keys(notSorted)
  .sort()
  .reduce(
    (acc, key) => ({
      ...acc,
      [key]: notSorted[key],
    }),
    {}
  );

console.log(sorted);

to call Object.keys with notSorted to get the keys of the notSorted object in an array.

Then we call sort to sort the keys.

Then we call reduce with the sorted keys array with a callback to get the keys and values and put them in a new object.

The new object with the sort keys is then returned by reduce.

Conclusion

To sort or order keys in JavaScript objects, we can use the Object.keys method and the array sort and reduce methods.

Categories
JavaScript Answers

How to sort an array so that null values always come last with JavaScript?

Sometimes, we want to sort an array so that null values always come last with JavaScript.

In this article, we’ll look at how to sort an array so that null values always come last with JavaScript.

How to sort an array so that null values always come last with JavaScript?

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

For instance, we write

const sorted = arr.sort(
  (a, b) => (a !== null ? a : Infinity) - (b !== null ? b : Infinity)
);

to call arr.sort with a callback that sorts non-null values in ascending order.

We check if a or b is null.

If they are, we return Infinity as its value and use that for sorting.

To sort in descending order, we flip the expressions in the callback by writing

const sorted = arr.sort(
  (a, b) => (b !== null ? b : -Infinity) - (a !== null ? a : -Infinity)
);

Conclusion

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

Categories
Python Answers

How to pass arguments into redirect(url_for()) of Flask?

Sometimes, we want to pass arguments into redirect(url_for()) of Flask.

In this article, we’ll look at how to pass arguments into redirect(url_for()) of Flask.

How to pass arguments into redirect(url_for()) of Flask?

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.

Then we can call url_for with the parameters.

For instance, we write

@app.route("/found/<email>/<list_of_objects>")
def found(email, list_of_objects):
    return render_template("found.html", keys=email, obj=list_of_objects)

to add the /found/<email>/<list_of_objects> route that maps to the found function.

In it, we get the the URL parameters from the found function’s parameters.

Then in another route, we write

return redirect(url_for("found", email=x, list_of_objects=y))

to call url_for with the route name and the parameters to return the URL for the /found/<email>/<list_of_objects> route with the parameters filled in.

Then we call redirect with the URL to redirect to the /found/<email>/<list_of_objects> route.

Conclusion

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.

Categories
Python Answers

How to debug a Python Flask app running in Gunicorn?

Sometimes, we want to debug a Python Flask app running in Gunicorn.

In this article, we’ll look at how to debug a Python Flask app running in Gunicorn.

How to debug a Python Flask app running in Gunicorn?

To debug a Python Flask app running in Gunicorn, we can set the DEBUG setting to True.

For instance, we write

app = Flask(__name__)
app.config.from_pyfile('config.py')

to read the config from config.py with from_pyfile.

Then we write

DEBUG = True

to set the DEBUG setting to True to enable debugging in our Flask app.

Conclusion

To debug a Python Flask app running in Gunicorn, we can set the DEBUG setting to True.