Categories
JavaScript Answers

How to create multidimensional array with JavaScript?

Sometimes, we want to create multidimensional array with JavaScript.

In this article, we’ll look at how to create multidimensional array with JavaScript.

How to create multidimensional array with JavaScript?

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.

For instance, we write

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];
numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

by putting arrays into arrays like

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];

We also assign values to the positions we want with

numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

where the first index is the position of the outer array and the 2nd is the position of the inner array.

Conclusion

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.

Categories
JavaScript Answers

How to format numbers with JavaScript?

Sometimes, we want to format numbers with JavaScript.

In this article, we’ll look at how to format numbers with JavaScript.

How to format numbers with JavaScript?

To format numbers with JavaScript, we call the number toLocaleString method.

For instance, we write

const value = (100000).toLocaleString(undefined, { minimumFractionDigits: 2 });
console.log(value);

to call toLocaleString with { minimumFractionDigits: 2 } to return a number string with the number rounded to 2 decimal places.

Conclusion

To format numbers with JavaScript, we call the number toLocaleString method.

Categories
JavaScript Answers

How to remove multiple elements from array in JavaScript?

Sometimes, we want to remove multiple elements from array in JavaScript.

In this article, we’ll look at how to remove multiple elements from array in JavaScript.

How to remove multiple elements from array in JavaScript?

To remove multiple elements from array in JavaScript, we can use the array filter method.

For instance, we write

let valuesArr = ["v1", "v2", "v3", "v4", "v5"];
const removeValFrom = [0, 2, 4];
valuesArr = valuesArr.filter((value, index) => {
  return removeValFrom.indexOf(index) === -1;
});

to call valueArr.filter with a callback that checks if the index of the element in valuesArr is in the removeValFrom array which has the indexes of the items in valuesArr that we want to remove.

A new array with the items that we want to keep are included, so we assign the array back to valuesArr.

Conclusion

To remove multiple elements from array in JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to call a JavaScript function every 5 seconds continuously?

Sometimes, we want to call a JavaScript function every 5 seconds continuously.

In this article, we’ll look at how to call a JavaScript function every 5 seconds continuously.

How to call a JavaScript function every 5 seconds continuously?

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.

For instance, we write

const interval = setInterval(() => {
  // ...
}, 5000);

clearInterval(interval);

to call setInterval with the callback we want to run and 5000 millisecond period.

Then the callback runs every 5 seconds.

Conclusion

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.

Categories
React Answers

How to fix Uncaught ReferenceError: React is not defined with React?

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

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

How to fix Uncaught ReferenceError: React is not defined with React?

To fix Uncaught ReferenceError: React is not defined with React, we can change the Babel config.

For instance, we write

{
  "presets": [
    "@babel/preset-env",
    ["@babel/preset-react", { "runtime": "automatic" }]
  ]
}

to enable the React presets to run automatically at runtime to add React to where it’s needed in our code in .babelrc.

This works with React 17 and Babel.

Conclusion

To fix Uncaught ReferenceError: React is not defined with React, we can change the Babel config.