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 JavaScript array’s reduce method.

For instance, we write:

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

const lookup = values.reduce((a, e) => {
  if (a[e.id]) {
    return {
      ...a,
      [e.id]: a[e.id] + 1
    }
  }
  return {
    ...a,
    [e.id]: 1
  }
}, {});

const dups = values.filter(e => lookup[e.id] > 1)
console.log(dups);

We call values.reduce with a callback that returns an object with the counts of the object give in values given the value of e.id.

If a[e.id] is defined, then we increment it by 1.

Otherwise, we set [e.id] to 1.

Then we get the id‘s of the objects in values with value bigger than 1.

As a result, dups is:

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

Conclusion

To get a list of duplicate objects in an array of objects with JavaScript, we can use the JavaScript array’s reduce method.

Categories
JavaScript Answers

How to insert or remove HTML content between div tags with JavaScript?

Sometimes, we want to insert or remove HTML content between div tags with JavaScript.

In this article, we’ll look at how to insert or remove HTML content between div tags with JavaScript.

How to insert or remove HTML content between div tags with JavaScript?

To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML property of the element.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<span>Something</span>';

to select the div with querySelector.

Then we set div.innerHTML to a string with a span to replace ‘hello world’ with the span.

Therefore, we should see ‘Something’ instead of ‘hello world’ displayed.

Conclusion

To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML property of the element.

Categories
JavaScript Answers

How to add toast notifications with toastr and JavaScript?

Sometimes, we want to add toast notifications with toastr and JavaScript.

In this article, we’ll look at how to add toast notifications with toastr and JavaScript.

How to add toast notifications with toastr and JavaScript?

To add toast notifications with toastr and JavaScript, we can add the toastr script and CSS.

Then we can use the toastr object to create toasts.

For instance, we write:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>

to add the toastr CSS and JavaScript file and jQuery.

jQuery is required for toastr.

Then we call toastr.success with a message string to show a success message by writing:

toastr.success('Success messages');

Conclusion

To add toast notifications with toastr and JavaScript, we can add the toastr script and CSS.

Then we can use the toastr object to create toasts.

Categories
JavaScript Answers

How to validate a ISO 8601 date using moment.js and JavaScript?

Sometimes, we want to validate a ISO 8601 date using moment.js and JavaScript.

In this article, we’ll look at how to validate a ISO 8601 date using moment.js and JavaScript.

How to validate a ISO 8601 date using moment.js and JavaScript?

To validate a ISO 8601 date using moment.js and JavaScript, we can use the isValid method.

For instance, we write:

const isValid = moment("2022-10-10T14:48:00", moment.ISO_8601).isValid()
console.log(isValid)

We call moment with the date string to parse and moment.ISO_8601 to parse the date string as an ISO 8601 date string.

Then we call isValid to return whether the date string is a valid date string according to the format we specified when parsing.

Therefore, isValid should be true since we the date string is a valid ISO 8601 date string.

Conclusion

To validate a ISO 8601 date using moment.js, we can use the isValid method.

Categories
JavaScript Answers

How to count the number of files in a directory using JavaScript and Node.js?

Sometimes, we want to count the number of files in a directory using JavaScript and Node.js.

In this article, we’ll look at how to count the number of files in a directory using JavaScript and Node.js.

How to count the number of files in a directory using JavaScript and Node.js?

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.

For instance, we write:

const { promises: fs } = require('fs');
const dir = '/';

const getNumFiles = async (dir) => {
  const files = await fs.readdir(dir)
  console.log(files.length)
}
getNumFiles(dir)

to define the getNumFiles function that takes the dir directory string.

In the function, we call fs.readdir with dir to read the files data in dir into an array.

Then we get the number of files from the length property.

Conclusion

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.