Categories
JavaScript Answers

How to extract the user name from an email address using JavaScript?

Sometimes, we want to extract the user name from an email address using JavaScript.

In this article, we’ll look at how to extract the user name from an email address using JavaScript.

How to extract the user name from an email address using JavaScript?

To extract the user name from an email address using JavaScript, we can use a regex to get the part before the @ with the string match method.

For instance, we write:

const str = "someone@example.com";
const nameMatch = str.match(/^([^@]*)@/);
console.log(nameMatch)

to call str.match with /^([^@]*)@/ to get the part of the email address before the @.

Therefore, nameMatch is ['someone@', 'someone', index: 0, input: 'someone@example.com', groups: undefined].

Conclusion

To extract the user name from an email address using JavaScript, we can use a regex to get the part before the @ with the string match method.

Categories
JavaScript Answers

How to retrieve the last day of the year in JavaScript?

Sometimes, we want to retrieve the last day of the year in JavaScript.

In this article, we’ll look at how to retrieve the last day of the year in JavaScript.

How to retrieve the last day of the year in JavaScript?

To retrieve the last day of the year in JavaScript, we can use the date methods.

For instance, we write:

const lastDayOfYear = new Date(new Date().getFullYear(), 11, 31)
console.log(lastDayOfYear)

to create a Date instance with the 4 digit year number of the current year that we get from getFullYear.

The 2nd argument is 11 for December and the last argument is 31 for the 31st day of the month.

Therefore, lastDayOfYear is Sat Dec 31 2022 00:00:00 GMT-0800 (Pacific Standard Time) if today’s year is 2022.

Conclusion

To retrieve the last day of the year in JavaScript, we can use the date methods.

Categories
JavaScript Answers

How to get a list of duplicate objects in an array of objects with JavaScript?

Sometimes, we want to get a list of duplicate objects in an array of objects with JavaScript.

In this article, we’ll look at how to get a list of duplicate objects in an array of objects with JavaScript.

How to get a list of duplicate objects in an array of objects with JavaScript?

To get a list of duplicate objects in an array of objects with JavaScript, we can use the array methods.

For instance, we write:

const values = [{
    id: 10,
    name: 'someName1'
  },
  {
    id: 10,
    name: 'someName2'
  },
  {
    id: 11,
    name: 'someName3'
  },
  {
    id: 12,
    name: 'someName4'
  }
];

const duplicateIds = values
  .map(v => v.id)
  .filter((v, i, vIds) => vIds.indexOf(v) !== i)
const duplicates = values
  .filter(obj => duplicateIds.includes(obj.id));
console.log(duplicates)

to get an array of value entries with the same id and put them into duplicates.

To do this, we get the ids of the items with the same id by calling map to get the ids into their own array.

Then we call filter with a callback that checks if indexOf returns the same value as index i to get all the ids of the duplicate items in the duplicateIds array.

Next, we call values.filter with a callback to check if the id of the item is in the duplicateIds array.

As a result, we get:

[
  {
    "id": 10,
    "name": "someName1"
  },
  {
    "id": 10,
    "name": "someName2"
  }
]

for the value of duplicates.

Conclusion

To get a list of duplicate objects in an array of objects with JavaScript, we can use the array methods.

Categories
JavaScript Answers

How to convert object’s properties and values to array of key value pairs with JavaScript?

Sometimes, we want to convert object’s properties and values to array of key value pairs with JavaScript.

In this article, we’ll look at how to convert object’s properties and values to array of key value pairs with JavaScript.

How to convert object’s properties and values to array of key value pairs with JavaScript?

To convert object’s properties and values to array of key value pairs with JavaScript, we can use the Object.entries method.

For instance, we write:

const obj = {
  value1: 'prop1',
  value2: 'prop2',
  value3: 'prop3'
};
const pairs = Object.entries(obj)
console.log(pairs)

We call Object.entries with obj to return an array of key-value arrays from the obj object.

As a result, pairs is:

[
  [
    "value1",
    "prop1"
  ],
  [
    "value2",
    "prop2"
  ],
  [
    "value3",
    "prop3"
  ]
]

The first entry of each nested array is the key and the 2nd entry is the value of the corresponding key.

Conclusion

To convert object’s properties and values to array of key value pairs with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to change the page title with JavaScript?

Sometimes, we want to change the page title with JavaScript.

In this article, we’ll look at how to change the page title with JavaScript.

How to change the page title with JavaScript?

To change the page title with JavaScript, we can set the document.title property.

For instance, we write:

document.title = 'New Title';

to set the page title to ‘New Title’.

Conclusion

To change the page title with JavaScript, we can set the document.title property.