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
TypeScript Answers

How to fix ‘instanceof’ in TypeScript give me the error “‘Foo’ only refers to a type, but is being used as a value here.” error?

Sometimes, we want to fix ‘instanceof’ in TypeScript give me the error "’Foo’ only refers to a type, but is being used as a value here." error.

In this article, we’ll look at how to fix ‘instanceof’ in TypeScript give me the error "’Foo’ only refers to a type, but is being used as a value here." error.

How to fix ‘instanceof’ in TypeScript give me the error "’Foo’ only refers to a type, but is being used as a value here." error?

To fix ‘instanceof’ in TypeScript give me the error "’Foo’ only refers to a type, but is being used as a value here." error, we should only use instanceof with constructors.

For instance, we write

interface Foo {
  abcdef: number;
}

let x: Foo | string;

if ('abcdef' in x) {
  // ...
}

to create the Foo instance.

And we check if x implements Foo with 'abcdef' in x.

If the abcdef property is in x, then we know x is a Foo object since strings don’t have the abcdef property and x is either a Foo object or a string.

Conclusion

To fix ‘instanceof’ in TypeScript give me the error "’Foo’ only refers to a type, but is being used as a value here." error, we should only use instanceof with constructors.

Categories
React Answers

How to fix ESLint error missing in props validation with React?

Sometimes, we want to fix ESLint error missing in props validation with React

In this article, we’ll look at how to fix ESLint error missing in props validation with React.

How to fix ESLint error missing in props validation with React?

To fix ESLint error missing in props validation with React, we can add a comment or disable the rule globally with a config.

For instance, we write

/* eslint-disable react/prop-types */

to disable prop type check for the line immediately below the comment in our code.

Or we can add

{
  //...
  "rules": {
    "react/prop-types": "off"
  }
  //...
}

in .eslintrc to disable the prop type validation rule for the whole project.

Conclusion

To fix ESLint error missing in props validation with React, we can add a comment or disable the rule globally with a config.

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.