Categories
JavaScript Answers

How to apply trim function to each string in an array with JavaScript?

Sometimes, we want to apply trim function to each string in an array with JavaScript.

In this article, we’ll look at how to apply trim function to each string in an array with JavaScript.

How to apply trim function to each string in an array with JavaScript?

To apply trim function to each string in an array with JavaScript, we can use the array map method.

For instance, we write

const trimmedArr = arr.map((s) => s.trim());

to call arr.map with a callback that returns the trimmed version of each string we get from calling s.trim.

And then we assign the returned array to trimmedArr.

Conclusion

To apply trim function to each string in an array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to create unique ID with JavaScript?

Sometimes, we want to create unique ID with JavaScript.

In this article, we’ll look at how to create unique ID with JavaScript.

How to create unique ID with JavaScript?

To create unique ID with JavaScript, we can use the Math.random method.

For instance, we write

const id = Math.random().toString(16).slice(2);

to call the Math.random method to return a random number between 0 and 1.

Then we call toString with 16 on the returned number to convert it to a hex string.

Next, we call slice with 2 to return the string from index 2 to the end of it.

Conclusion

To create unique ID with JavaScript, we can use the Math.random method.

Categories
JavaScript Answers

How to fix Error: Cannot resolve module ‘style-loader’ with JavaScript?

Sometimes, we want to fix Error: Cannot resolve module ‘style-loader’ with JavaScript.

In this article, we’ll look at how to fix Error: Cannot resolve module ‘style-loader’ with JavaScript.

How to fix Error: Cannot resolve module ‘style-loader’ with JavaScript?

To fix Error: Cannot resolve module ‘style-loader’ with JavaScript, we install style-loader and add it to the Webpack config.

To install it, we run

npm install style-loader css-loader --save

to install the style-loader and css-loader packages.

Then in our Webpack config file, we write

module.exports = {
  //...
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        include: path.join(_dirname, "app"),
      },
      {
        test: /\.css$/,
        loader: "style-loader!css-loader",
      },
    ],
    resolve: {
      extensions: ["", ".js", ".jsx", ".css"],
    },
  },
};

to use the css-loader for importing CSS fiiles.

Conclusion

To fix Error: Cannot resolve module ‘style-loader’ with JavaScript, we install style-loader and add it to the Webpack config.

Categories
JavaScript Answers

How to save canvas as png image with JavaScript?

Sometimes, we want to save canvas as png image with JavaScript.

In this article, we’ll look at how to save canvas as png image with JavaScript.

How to save canvas as png image with JavaScript?

To save canvas as png image with JavaScript, we can use the FileSaver.js library.

We install it with

npm install file-saver --save

Then we use it by writing

import { saveAs } from "file-saver";

const canvas = document.getElementById("my-canvas");

//...

canvas.toBlob((blob) => {
  saveAs(blob, "image.png");
});

We select the canvas with getElementById.

Then we call canvas.toBlob with a callback that calls saveAs to save the blob with the canvas content to image.png.

Conclusion

To save canvas as png image with JavaScript, we can use the FileSaver.js library.

Categories
JavaScript Answers

How to get an object’s methods with JavaScript?

Sometimes, we want to get an object’s methods with JavaScript.

In this article, we’ll look at how to get an object’s methods with JavaScript.

How to get an object’s methods with JavaScript?

To get an object’s methods with JavaScript, we can use the array filter method and Object.keys method.

For instance, we write

const myObj = { myFn() {}, abc: true };
const allKeys = Object.keys(myObj);
const fnKeys = allKeys.filter((key) => typeof myObj[key] == "function");
console.log(fnKeys);

to call Object.keys with myObj to get an array of property kets in myObj as a string array.

Then we call allKeys.filter with a callback that checks if myObj[key] is a function with the typeof operator.

And an array of method keys are returned by filter.

Conclusion

To get an object’s methods with JavaScript, we can use the array filter method and Object.keys method.