Categories
JavaScript Answers

How to Convert Latitude and Longitude to Decimal Values with JavaScript?

Sometimes, we want to convert latitude and longitude to decimal values with JavaScript.

In this article, we’ll look at how to convert latitude and longitude to decimal values with JavaScript.

Convert Latitude and Longitude to Decimal Values with JavaScript

To convert latitude and longitude to decimal values with JavaScript, we can extract the numerical and directional parts of the latitude and longitude values into an array.

Then we can calculate the coordinate values from the extracted values.

For instance, we write:

const convertDMSToDD = (degrees, minutes, seconds, direction) => {
  let dd = +degrees + +minutes / 60 + +seconds / (60 * 60);
  if (direction === "S" || direction === "W") {
    dd = dd * -1;
  }
  return dd;
}

const parseDMS = (input) => {
  const parts = input.split(/[^\d\w]+/);
  const lat = convertDMSToDD(parts[0], parts[1], parts[2], parts[3]);
  const lng = convertDMSToDD(parts[4], parts[5], parts[6], parts[7]);
  return [lat, lng]
}

const coords = parseDMS(`36°57'9" N 110°4'21" W`)
console.log(coords)

to create the convertDMSToDD function that takes the degrees, minutes, seconds, and direction parameters.

Then we get the number part of the coordinates value with +degrees + +minutes / 60 + +seconds / (60 * 60) and assign the returned value to dd.

We convert number string to a number with the unary + operator.

If direction is 'S' or 'W', we multiply the dd value by -1.

And then we return dd.

Next, we create the parseDMS function with the input string.

We call split to split the string into numbers and the direction parts with /[^\d\w]+/.

Then we call convertDMSToDD with the extracted parts and return the values.

Therefore, from the console log, we see that coords is [36.9525, -110.07249999999999].

Conclusion

To convert latitude and longitude to decimal values with JavaScript, we can extract the numerical and directional parts of the latitude and longitude values into an array.

Then we can calculate the coordinate values from the extracted values.

Categories
JavaScript Answers

How to Use document.getElementById with Multiple IDs in JavaScript?

Sometimes, we want to use document.getElementById with Multiple IDs in JavaScript

In this article, we’ll look at how to use document.getElementById with Multiple IDs in JavaScript

Use document.getElementById with Multiple IDs in JavaScript

To use document.getElementById with Multiple IDs in JavaScript, we can replace document.getElementById with document.querySelector.

For instance, if we have:

<div id='myCircle1'>
  circle
</div>
<div id='myCircle2'>
  circle
</div>
<div id='myCircle3'>
  circle
</div>
<div id='myCircle4'>
  circle
</div>

Then we write:

const circles = document.querySelectorAll("#myCircle1, #myCircle2, #myCircle3, #myCircle4")
console.log(circles)

We call document.querySelectorAll with a string that has the selector of all the elements separated by a comma.

And then we assign the returned result to circles.

Therefore, circles is a node list with the 4 elements we selected.

Conclusion

To use document.getElementById with Multiple IDs in JavaScript, we can replace document.getElementById with document.querySelector.

Categories
JavaScript Answers

How to Copy a Text String on Click with JavaScript?

Sometimes, we want to copy a text string on click with JavaScript.

In this article, we’ll look at how to copy a text string on click with JavaScript.

Copy a Text String on Click with JavaScript

To copy a text string on click with JavaScript, we can call the document.execuCommand to copy the text to the clipboard.

Then we can listen to the copy event to get the copied text.

For instance, if we have:

<span>hello world</span>

Then we write:

const span = document.querySelector("span");

span.onclick = () => {
  document.execCommand("copy");
}

span.addEventListener("copy", (event) => {
  event.preventDefault();
  if (event.clipboardData) {
    event.clipboardData.setData("text/plain", span.textContent);
    console.log(event.clipboardData.getData("text"))
  }
});

We select the span element with document.querySelector.

Then we set the span.onclick property to a function that calls document.execCommand with 'copy' to copy the text content of the span.

Next, we call span.addEventListener with 'copy' to add a copy event listener.

The callback will run when we run document.execCommand("copy");.

We get the clipboard data with the event.clipboardData.setData to set the copied data to the textContent of the span.

And then we can get the text data that’s copied to the clipboatd with the getData method with 'text'.

Conclusion

To copy a text string on click with JavaScript, we can call the document.execuCommand to copy the text to the clipboard.

Then we can listen to the copy event to get the copied text.

Categories
JavaScript Answers

How to Sort a JavaScript Array by ISO 8601 Date?

Sometimes, we want to sort a JavaScript array by ISO 8601 date.

In this article, we’ll look at how to sort a JavaScript array by ISO 8601 date.

Sort a JavaScript Array by ISO 8601 Date

To sort a JavaScript array by ISO 8601 date, we can use the JavaScript array’s sort method.

For instance, we can write:

const arr = [{
    name: 'oldest',
    date: '2001-01-17T08:00:00Z'
  },
  {
    name: 'newest',
    date: '2011-01-28T08:00:00Z'
  },
  {
    name: 'old',
    date: '2009-11-25T08:00:00Z'
  }
];

const sorted = arr.sort((a, b) => {
  return (a.date < b.date) ? -1 : ((a.date > b.date) ? 1 : 0)
});
console.log(sorted)

We have the arr array that we want to sort by the date value.

Then we call arr.sort with a callback that compares the strings to compare the date strings directly by their values lexigraphically.

If a.date is less than b.date we return keep their order.

Otherwise, if a.date is bigger than b.date, we return 1 to reverse their order.

If they’re the same, we return 0.

Therefore, sorted, is:

[
  {
    "name": "oldest",
    "date": "2001-01-17T08:00:00Z"
  },
  {
    "name": "old",
    "date": "2009-11-25T08:00:00Z"
  },
  {
    "name": "newest",
    "date": "2011-01-28T08:00:00Z"
  }
]

Conclusion

To sort a JavaScript array by ISO 8601 date, we can use the JavaScript array’s sort method.

Categories
JavaScript Answers

How to Convert JavaScript Object to URL Parameters?

Sometimes, we want to convert JavaScript object to URL parameters.

In this article, we’ll look at how to convert JavaScript object to URL parameters.

Convert JavaScript Object to URL Parameters

To convert JavaScript object to URL parameters, we can use the jQuery.params method.

For instance, we write:

const params = {
  width: 1000,
  height: 800
};
const str = jQuery.param(params);
console.log(str);

We have the params object with the width and height properties.

Then we call jQuery.params method with the params object as its argument.

And then we assigned the returned query string to str.

From the console log, we should see that str is 'width=1000&height=800'.

Conclusion

To convert JavaScript object to URL parameters, we can use the jQuery.params method.