Categories
JavaScript Answers

How to remove hyphens from a string with JavaScript?

Sometimes, we want to remove hyphens from a string with JavaScript.

In this article, we’ll look at how to remove hyphens from a string with JavaScript.

How to remove hyphens from a string with JavaScript?

To remove hyphens from a string with JavaScript, we can use the string replace method.

For instance, we write

const str = "185-51-671";
const newStr = str.replace(/-/g, "");

to call str.replace with /-/g and an empty string to find all hyphens in str and replace them with empty strings.

The new string is returned.

Conclusion

To remove hyphens from a string with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to fix Lodash debounce not working in anonymous function with JavaScript?

Sometimes, we want to fix Lodash debounce not working in anonymous function with JavaScript.

In this article, we’ll look at how to fix Lodash debounce not working in anonymous function with JavaScript.

How to fix Lodash debounce not working in anonymous function with JavaScript?

To fix Lodash debounce not working in anonymous function with JavaScript, we call the function that debounce returns.

For instance, we write

_.debounce(debounceIt, 500, false)();

to call debounce with the debounceIt and the debounce interval to return a debounced function.

Then we call it immediately with () at the end.

Conclusion

To fix Lodash debounce not working in anonymous function with JavaScript, we call the function that debounce returns.

Categories
JavaScript Answers

How to join an array by a comma and a space with JavaScript?

Sometimes, we want to join an array by a comma and a space with JavaScript.

In this article, we’ll look at how to join an array by a comma and a space with JavaScript.

How to join an array by a comma and a space with JavaScript?

To join an array by a comma and a space with JavaScript, we can use the array join method.

For instance, we write

const myArray = [
  "css",
  "html",
  "xhtml",
  "html5",
  "css3",
  "javascript",
  "jquery",
  "lesscss",
  "arrays",
  "wordpress",
  "facebook",
  "fbml",
  "table",
  ".htaccess",
  "php",
  "c",
  ".net",
  "c#",
  "java",
];
const myString = myArray.join(", ");

to call myArray.join with ', ' to combine each element in myArray into a string separated by a comma and a space.

The combined string is returned.

Conclusion

To join an array by a comma and a space with JavaScript, we can use the array join method.

Categories
JavaScript Answers

How to download file from bytes in JavaScript?

Sometimes, we want to download file from bytes in JavaScript.

In this article, we’ll look at how to download file from bytes in JavaScript.

How to download file from bytes in JavaScript?

To download file from bytes in JavaScript, we can create an object URL from a blob and then download with the URL.

For instance, we write

const base64ToArrayBuffer = (base64) => {
  const binaryString = window.atob(base64);
  const binaryLen = binaryString.length;
  const bytes = new Uint8Array(binaryLen);
  for (let i = 0; i < binaryLen; i++) {
    const ascii = binaryString.charCodeAt(i);
    bytes[i] = ascii;
  }
  return bytes;
};

const saveByteArray = (fileName, byte) => {
  const blob = new Blob([byte], { type: "application/pdf" });
  const link = document.createElement("a");
  link.href = window.URL.createObjectURL(blob);
  link.download = fileName;
  link.click();
};

const sampleArr = base64ToArrayBuffer(data);
saveByteArray("Sample Report.pdf", sampleArr);

to define the base64ToArrayBuffer function.

In it, we take the base64 string and convert it to the bytes array.

We call window.atob to convert the base64 string to a byte string.

And then we create the bytes array from the binaryString by using a for loop to get the characters with charCodeAt.

Next, we define the saveByteArray function that takes the fileName and byte array object.

In it, we create a blob from byte with the Blob constructor.

We set the type property to the MIME type of the downloaded file.

Then we create an a element with createElement.

And then we call window.URL.createObjectURL with blob to create an object URL and set it as the value of href.

We set the download property to fileName.

Then we call click to start the download.

Conclusion

To download file from bytes in JavaScript, we can create an object URL from a blob and then download with the URL.

Categories
JavaScript Answers

How to get width height of remote image from URL with JavaScript?

Sometimes, we want to get width height of remote image from URL with JavaScript.

In this article, we’ll look at how to get width height of remote image from URL with JavaScript.

How to get width height of remote image from URL with JavaScript?

To get width height of remote image from URL with JavaScript, we can use the naturalWidth and naturalHeight properties.

For instance, we write

const img = new Image();
img.addEventListener("load", () => {
  console.log(img.naturalWidth, img.naturalHeight);
});
img.src = url;

to create an img element with the Image constructor.

Then we add an event listener for the load event with addEventListener.

In it, we get the width and height of the image with naturalWidth and naturalHeight.

Then we set img.src to url to load the image and run the load event handler.

Conclusion

To get width height of remote image from URL with JavaScript, we can use the naturalWidth and naturalHeight properties.