Categories
JavaScript Answers

How to count duplicate value in an array in JavaScript?

Sometimes, we want to count duplicate value in an array in JavaScript.

In this article, we’ll look at how to count duplicate value in an array in JavaScript.

How to count duplicate value in an array in JavaScript?

To count duplicate value in an array in JavaScript, we can put the count of each item in the array in an object.

For instance, we write

const counts = {};
const sampleArray = ["a", "a", "b", "c"];
sampleArray.forEach((x) => {
  counts[x] = (counts[x] ?? 0) + 1;
});
console.log(counts);

to loop through the sampleArray array by calling sampleArray.forEach with a callback that sets the counts[x] property to 0 if it doesn’t exist.

Or we add 1 to counts[x] if it already exists.

We do both with

counts[x] = (counts[x] ?? 0) + 1

Conclusion

To count duplicate value in an array in JavaScript, we can put the count of each item in the array in an object.

Categories
JavaScript Answers

How to import jQuery using ES6 syntax with JavaScript?

Sometimes, we want to import jQuery using ES6 syntax with JavaScript.

In this article, we’ll look at how to import jQuery using ES6 syntax with JavaScript.

How to import jQuery using ES6 syntax with JavaScript?

To import jQuery using ES6 syntax with JavaScript, we can use import.

For instance, we write

import { $, jQuery } from "jquery";

window.$ = $;
window.jQuery = jQuery;

to import $ and jQuery from the jquery module.

And then we assign $ and jQuery to properties of window with

window.$ = $;
window.jQuery = jQuery;

to make them global.

Conclusion

To import jQuery using ES6 syntax with JavaScript, we can use import.

Categories
JavaScript Answers

How to create an Asynchronous function in JavaScript?

Sometimes, we want to create an Asynchronous function in JavaScript.

In this article, we’ll look at how to create an Asynchronous function in JavaScript.

How to create an Asynchronous function in JavaScript?

To create an Asynchronous function in JavaScript, we can use a promise.

For instance, we write

const asyncFunct = new Promise((resolve, reject) => {
  //...
  resolve();
});

to create the asyncFunct promise with the Promise constructor.

We call Promise with a callback that calls resolve when our async code is finished.

Then we use await asyncFunct; to wait for the promise to finish before we run the code below it.

Conclusion

To create an Asynchronous function in JavaScript, we can use a promise.

Categories
JavaScript Answers

How to add Document with Custom ID to Firestore with JavaScript?

Sometimes, we want to add Document with Custom ID to Firestore with JavaScript

In this article, we’ll look at how to add Document with Custom ID to Firestore with JavaScript.

How to add Document with Custom ID to Firestore with JavaScript?

To add Document with Custom ID to Firestore with JavaScript, we call the set method.

For instance, we write

db.collection("cities").doc("LA").set({
  name: "Los Angeles",
  state: "CA",
  country: "USA",
});

to call set to add a document to the cities collection.

We call set with an object with the properties we want to add in a document.

We call doc with the ID of the document before we call set to populate its contents.

Conclusion

To add Document with Custom ID to Firestore with JavaScript, we call the set method.

Categories
JavaScript Answers

How to swap key with value in object with JavaScript?

Sometimes, we want to swap key with value in object with JavaScript.

In this article, we’ll look at how to swap key with value in object with JavaScript.

How to swap key with value in object with JavaScript?

To swap key with value in object with JavaScript, we can us the Object.entries and Object.fromEntries methods.

For instance, we write

const f = (obj) =>
  Object.fromEntries(Object.entries(obj).map((a) => a.reverse()));

to call Object.entries to return an array of key-value pair arrays.

Then we call map with a callback to reverse the key and value in each nested array with reverse.

Next, we convert the mapped nested array back to an object with Object.fromEntries.

Conclusion

To swap key with value in object with JavaScript, we can us the Object.entries and Object.fromEntries methods.