Categories
JavaScript Answers

How to convert base64 png data to JavaScript file objects?

Sometimes, we want to convert base64 png data to JavaScript file objects.

In this article, we’ll look at how to convert base64 png data to JavaScript file objects.

How to convert base64 png data to JavaScript file objects?

To convert base64 png data to JavaScript file objects, we use fetch.

For instance, we write

const res = await fetch(url);
const buf = await res.arrayBuffer();
const file = new File([buf], filename, { type: mimeType });

to call fetch with the image url to make a get request to get the image.

Then we get the image as an array buffer with arrayBuffer.

Next we put the array buffer into an array in the File constructor to convert the array buffer to a file with MIME type mimeType.

Conclusion

To convert base64 png data to JavaScript file objects, we use fetch.

Categories
JavaScript Answers

How to check if a string contains any element of an array in JavaScript?

Sometimes,. we want to check if a string contains any element of an array in JavaScript.

In this article, we’ll look at how to check if a string contains any element of an array in JavaScript.

How to check if a string contains any element of an array in JavaScript?

To check if a string contains any element of an array in JavaScript, we call the array some method.

For instance, we write

const arr = ["banana", "monkey banana", "apple", "kiwi", "orange"];

const checker = (value) =>
  !["banana", "apple"].some((element) => value.includes(element));

console.log(arr.filter(checker));

to call arr.filter with a callback that checks any of 'banana' or 'apple' isn’t in the value of element being looped through in the arr array.

As a result, filter returns an array with values that don’t have 'banana' or 'apple' in arr.

Conclusion

To check if a string contains any element of an array in JavaScript, we call the array some method.

Categories
JavaScript Answers

How to add an InfoWindow to each marker with Google Maps API v3 and JavaScript?

To add an InfoWindow to each marker with Google Maps API v3 and JavaScript, we call info.open.

For instance, we write

const marker = new google.maps.Marker({
  map,
  position: point,
  clickable: true,
});

marker.info = new google.maps.InfoWindow({
  content: "<b>Speed:</b> " + values.inst + " knots",
});

google.maps.event.addListener(marker, "click", () => {
  marker.info.open(map, marker);
});

set the marker.info property to an info window.

Then we call marker.info.open to open the info window when we click on the marker with addListener.

Categories
JavaScript Answers

How to remove square brackets in string using regex with JavaScript?

Sometimes, we want to remove square brackets in string using regex with JavaScript.

In this article, we’ll look at how to remove square brackets in string using regex with JavaScript.

How to remove square brackets in string using regex with JavaScript?

To remove square brackets in string using regex with JavaScript, we call the string replace method.

For instance, we write

console.log("['abc','xyz']".replace(/[\[\]']+/g, ""));

to call replace with a regex that matches opening and closing square brackets with \[ and \].

We use the g flag to replace all matches with empty strings.

The string with the replacements done is returned.

Conclusion

To remove square brackets in string using regex with JavaScript, we call the string replace method.

Categories
JavaScript Answers

How to send a https request to a rest service in Node.js and JavaScript?

Sometimes, we want to send a https request to a rest service in Node.js and JavaScript.

In this article, we’ll look at how to send a https request to a rest service in Node.js and JavaScript.

How to send a https request to a rest service in Node.js and JavaScript?

To send a https request to a rest service in Node.js and JavaScript, we use the https module.

For instance, we write

const https = require("https");

const options = {
  host: "www.example.com",
  port: 443,
  path: "/upload",
  method: "POST",
};

const req = https.request(options, (res) => {
  console.log("STATUS: ", res.statusCode);
  console.log("HEADERS: ", JSON.stringify(res.headers));
  res.setEncoding("utf8");
  res.on("data", (chunk) => {
    console.log("BODY: ", chunk);
  });
});

req.on("error", (e) => {
  console.log("problem with request: ", e.message);
});

to call https.request with the options object, which includes the URL, port, and the request method.

Then we get the response statusCode, headers, and the response body chunk in the request callback.

We listen for errors by calling req.on with 'error' and a error handler callback.

Conclusion

To send a https request to a rest service in Node.js and JavaScript, we use the https module.