Categories
JavaScript Answers

How to parse date time string in dd.mm.yyyy format with JavaScript?

Sometimes, we want to parse date time string in dd.mm.yyyy format with JavaScript.

In this article, we’ll look at how to parse date time string in dd.mm.yyyy format with JavaScript.

How to parse date time string in dd.mm.yyyy format with JavaScript?

To parse date time string in dd.mm.yyyy format with JavaScript, we can call the string split method.

For instance, we write

const strDate = "03.09.1979";
const [day, month, year] = strDate.split(".");
const date = new Date(+year, +month - 1, +day);

to call strDate.split to split the strDate string by the period.

Then we get the day, month, and year from the split string.

Next, we pass the 3 values into the Date constructor to create a date object from them.

We subtract month by 1 before we pass it into the Date constructor since it accepts a month that starts with 0 for January.

We use + to convert each value to a number before we pass them in as arguments.

Conclusion

To parse date time string in dd.mm.yyyy format with JavaScript, we can call the string split method.

Categories
JavaScript Answers

How to find index of an object by key and value in a JavaScript array?

Sometimes, we want to find index of an object by key and value in a JavaScript array.

In this article, we’ll look at how to find index of an object by key and value in a JavaScript array.

How to find index of an object by key and value in a JavaScript array?

To find index of an object by key and value in a JavaScript array, we can use the array findIndex method.

For instance, we write

const peoples = [
  { attr1: "bob", attr2: "pizza" },
  { attr1: "john", attr2: "sushi" },
  { attr1: "larry", attr2: "hummus" },
];

const index = peoples.findIndex((p) => p.attr1 == "john");

to call peoples.findIndex to find the index of the object in the peoples array with the attr1 property set to 'john'.

Conclusion

To find index of an object by key and value in a JavaScript array, we can use the array findIndex method.

Categories
JavaScript Answers

How to replace the last character of string using JavaScript?

Sometimes, we want to replace the last character of string using JavaScript.

In this article, we’ll look at how to replace the last character of string using JavaScript.

How to replace the last character of string using JavaScript?

To replace the last character of string using JavaScript, we can use the string replace method.

For instance, we write

const str1 = "Notion,Data,Identity,".replace(/.$/, ".");

to get the last character with the /.$/.

$ matches the end of a word.

And we replace it with a '.' with replace.

Conclusion

To replace the last character of string using JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to use JavaScript to limit a number between a min/max value?

Sometimes, we want to use JavaScript to limit a number between a min/max value.

In this article, we’ll look at how to use JavaScript to limit a number between a min/max value.

How to use JavaScript to limit a number between a min/max value?

To use JavaScript to limit a number between a min/max value, we can use the Math.min and Math.max methods.

For instance, we write

const limitNumberWithinRange = (num, min = 1, max = 20) => {
  const parsed = parseInt(num);
  return Math.min(Math.max(parsed, min), max);
};

to define the limitNumberWithinRange function.

In it, we parse num into an integer with parseInt.

Then we call Math.max with parsed and min to return the max between parsed and min.

Then we get the lower number from Math.max(parsed, min) and max with Math.min.

If parsed is less than min, then min is returned.

And if parsed is bigger than max, then max is returned.

Conclusion

To use JavaScript to limit a number between a min/max value, we can use the Math.min and Math.max methods.

Categories
JavaScript Answers

How to remove object properties with Lodash and JavaScript?

Sometimes, we want to remove object properties with Lodash.

In this article, we’ll look at how to remove object properties with Lodash.

How to remove object properties with Lodash?

To remove object properties with Lodash, we can use the pick function.

For instance, we write

const model = {
  fName: null,
  lName: null,
};

const credentials = {
  fName: "xyz",
  lName: "abc",
  age: 23,
};

const result = _.pick(credentials, _.keys(model));

console.log(result);

to get the array of property keys from the model object with the keys function.

Then we call pick with credentials and the array of keys returned by the keys function to return a new object with the properties with the property names that are in the model object.

Conclusion

To remove object properties with Lodash, we can use the pick function.