Categories
JavaScript Answers

How to remove duplicate objects from JSON array with JavaScript?

Sometimes, we want to remove duplicate objects from JSON array with JavaScript.

In this article, we’ll look at how to remove duplicate objects from JSON array with JavaScript.

How to remove duplicate objects from JSON array with JavaScript?

To remove duplicate objects from JSON array with JavaScript, we can use a set.

For instance, we write

const data = [
  { Grade: "Math K", Domain: "Counting & Cardinality" },
  { Grade: "Math K", Domain: "Geometry" },
  { Grade: "Math 1", Domain: "Counting & Cardinality" },
  { Grade: "Math 1", Domain: "Orders of Operation" },
  { Grade: "Math 2", Domain: "Geometry" },
];
const set = new Set(data.map((item) => JSON.stringify(item)));
const dedup = [...set].map((item) => JSON.parse(item));

to call data.map to map each object in data to JSON strings with JSON.stringify.

Then we put them all in a set with the Set constructor.

Next, we spread the set back to an array then call map to map them back to objects with JSON.parse.

Conclusion

To remove duplicate objects from JSON array with JavaScript, we can use a set.

Categories
JavaScript Answers

How to use a for loop in multidimensional JavaScript array?

Sometimes, we want to use a for loop in multidimensional JavaScript array.

In this article, we’ll look at how to use a for loop in multidimensional JavaScript array.

How to use a for loop in multidimensional JavaScript array?

To use a for loop in multidimensional JavaScript array, we use a for-of loop.

For instance, we write

const arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9, 10],
];

for (const i of arr) {
  for (const j of i) {
    console.log(j);
  }
}

to use a nested for-of loop to loop through the elements.

We loop through the outer array with the outer loop.

Then we loop through the elements in each nested array with the inner loop.

i is the entry in the outer array being looped through.

j has the value of each entry in the nested array.

Conclusion

To use a for loop in multidimensional JavaScript array, we use a for-of loop.

Categories
JavaScript Answers

How to get list of all input objects using JavaScript, without accessing a form object?

Sometimes, we want to get list of all input objects using JavaScript, without accessing a form object.

In this article, we’ll look at how to get list of all input objects using JavaScript, without accessing a form object.

How to get list of all input objects using JavaScript, without accessing a form object?

To get list of all input objects using JavaScript, without accessing a form object, we use getElementsByTagName.

For instance, we write

const inputs = document.getElementsByTagName("input");
for (const input of inputs) {
  // ...
}

to select all input elements with getElementsByTagName.

It returns a node list with all the elements, which we use a for-of loop to loop through.

Conclusion

To get list of all input objects using JavaScript, without accessing a form object, we use getElementsByTagName.

Categories
React Answers

How to fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript?

Sometimes, we want to fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript.

In this article, we’ll look at how to fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript.

How to fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript?

To fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript, we should make sure we import it.

For instance, we write

import ReactDOM from "react-dom";

to import it.

Conclusion

To fix Uncaught ReferenceError: ReactDOM is not defined with JavaScript, we should make sure we import it.

Categories
JavaScript Answers

How to determine if the current browser is Firefox on a computer with JavaScript?

Sometimes, we want to determine if the current browser is Firefox on a computer with JavaScript.

In this article, we’ll look at how to determine if the current browser is Firefox on a computer with JavaScript.

How to determine if the current browser is Firefox on a computer with JavaScript?

To determine if the current browser is Firefox on a computer with JavaScript, we check the user agent string.

For instance, we write

if (navigator.userAgent.indexOf("Firefox") > 0) {
  console.log("firefox");
}

to get the user agent string with navigator.userAgent.

Then we check if 'Firefox' is in the string with indexOf.

Conclusion

To determine if the current browser is Firefox on a computer with JavaScript, we check the user agent string.