Categories
JavaScript Answers

How to get column from a two dimensional array with JavaScript?

Sometimes, we want to get column from a two dimensional array with JavaScript.

In this article, we’ll look at how to get column from a two dimensional array with JavaScript.

How to get column from a two dimensional array with JavaScript?

To get column from a two dimensional array with JavaScript, we can use the array map method.

For instance, we write

const arrayColumn = (arr, n) => arr.map((x) => x[n]);

const twoDimensionalArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(arrayColumn(twoDimensionalArray, 0));

to define the arrayColumn function that return an array with the items from column n in the 2D array arr.

We call map with a callback that returns the item at index n of each array in arr.

Then we call arrayColumn with twoDimensionalArray and 0 to get [1, 4, 7].

Conclusion

To get column from a two dimensional array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to check for duplicate strings in JavaScript array?

Sometimes, we want to check for duplicate strings in JavaScript array.

In this article, we’ll look at how to check for duplicate strings in JavaScript array.

How to check for duplicate strings in JavaScript array?

To check for duplicate strings in JavaScript array, we can use a set.

For instance, we write

const checkIfDuplicateExists = (arr) => {
  return new Set(arr).size !== arr.length;
};

const arr = ["a", "a", "b", "c"];

console.log(checkIfDuplicateExists(arr));

to define the checkIfDuplicateExists function to check if the arr converted to a set has the same size as the original arr array.

If they have the same size, then there’re no duplicate values in arr since sets can’t have duplicate values but arrays can.

Conclusion

To check for duplicate strings in JavaScript array, we can use a set.

Categories
JavaScript Answers

How to check if input date is equal to today’s date with JavaScript?

Sometimes, we want to check if input date is equal to today’s date with JavaScript.

In this article, we’ll look at how to check if input date is equal to today’s date with JavaScript.

How to check if input date is equal to today’s date with JavaScript?

To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.

For instance, we write

const inputDate = new Date("11/21/2022");
const todaysDate = new Date();

if (inputDate.setHours(0, 0, 0, 0) === todaysDate.setHours(0, 0, 0, 0)) {
  // ...
}

to compare the timestamps of inputDate and todaysDate after we call setHours on each to set their times to midnight.

When we use === to compare them, their timestamps are compared.

So if their timestamps are the same, then they’re the same date.

Conclusion

To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.

Categories
JavaScript Answers

How to check if a number is negative with JavaScript?

Sometimes, we want to check if a number is negative with JavaScript.

In this article, we’ll look at how to check if a number is negative with JavaScript.

How to check if a number is negative with JavaScript?

To check if a number is negative with JavaScript, we use the Math.sign method.

For instance, we write

const number = 1;

if (Math.sign(number) === 1) {
  console.log("I'm positive");
} else if (Math.sign(number) === -1) {
  console.log("I'm negative");
} else {
  console.log("I'm not a number");
}

to call Math.sign with number.

Since 1 positive, Math.sign returns 1.

If a number is negative, it returns -1.

If the number is positive zero, returns 0.

If the number is negative zero, returns -0.

Otherwise, NaN is returned.

Conclusion

To check if a number is negative with JavaScript, we use the Math.sign method.

Categories
JavaScript Answers

How to get image from canvas element and use it in img src tag with JavaScript?

Sometimes, we want to get image from canvas element and use it in img src tag with JavaScript.

In this article, we’ll look at how to get image from canvas element and use it in img src tag with JavaScript.

How to get image from canvas element and use it in img src tag with JavaScript?

To get image from canvas element and use it in img src tag with JavaScript, we can use the canvas toDataURL method.

For instance, we write

const image = new Image();
image.id = "pic";
image.src = canvas.toDataURL();
document.getElementById("image-for-crop").appendChild(image);

to call canas.toDataURL to return the canvas image a base64 URL string.

Then we set its as the src attribute value of the image img element to show the image in the base64 URL.

Conclusion

To get image from canvas element and use it in img src tag with JavaScript, we can use the canvas toDataURL method.