Categories
JavaScript Answers

How to use lodash to compare jagged arrays with JavaScript?

Sometimes, we want to use lodash to compare jagged arrays with JavaScript.

In this article, we’ll look at how to use lodash to compare jagged arrays with JavaScript.

How to use lodash to compare jagged arrays with JavaScript?

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

For instance, we write

const array1 = [
  ["a", "b"],
  ["b", "c"],
];
const array2 = [
  ["b", "c"],
  ["a", "b"],
];
_.isEqual(array1.sort(), array2.sort());

to call sort to sort both arrays.

And then we call Lodash’s isEqual function to compare the contents of the sorted arrays.

Conclusion

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

Categories
TypeScript Answers

How to mock dependency in Jest with TypeScript?

Sometimes, we want to mock dependency in Jest with TypeScript.

In this article, we’ll look at how to mock dependency in Jest with TypeScript.

How to mock dependency in Jest with TypeScript?

To mock dependency in Jest with TypeScript, we set the type of the mocked dependency to jest.Mock<typeof dep.default>.

For instance, we write

import * as dep from "../dependency";
jest.mock("../dependency");

const mockedDependency = <jest.Mock<typeof dep.default>>dep.default;

it("should do what I need", () => {
  mockedDependency.mockReturnValueOnce("return");
});

to get the cast the type of dep.default to jest.Mock<typeof dep.default>.

Then we can call mockReturnValueOnce without TypeScript compiler errors.

Conclusion

To mock dependency in Jest with TypeScript, we set the type of the mocked dependency to jest.Mock<typeof dep.default>.

Categories
JavaScript Answers

How to convert a string to JSON object with JavaScript?

Sometimes, we want to convert a string to JSON object with JavaScript.

In this article, we’ll look at how to convert a string to JSON object with JavaScript.

How to convert a string to JSON object with JavaScript?

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

For instance, we write

const obj = JSON.parse(string);

to call JSON.parse with the JSON string to convert the JSON string to an object.

Conclusion

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

Categories
React Answers

How to add images in public folder with React?

Sometimes, we want to add images in public folder with React.

In this article, we’ll look at how to add images in public folder with React.

How to add images in public folder with React?

To add images in public folder with React, we get the path to the public folder with process.env.PUBLIC_URL and concatenate the relative image path to it.

For instance, we write

<img src={process.env.PUBLIC_URL + "/img/logo.png"} />;

to add the img element with the src attribute of the image set to process.env.PUBLIC_URL + "/img/logo.png" to load the image from the /img/logo.png file in the public folder.

We use process.env.PUBLIC_URL to get the path to the public folder.

Conclusion

To add images in public folder with React, we get the path to the public folder with process.env.PUBLIC_URL and concatenate the relative image path to it.

Categories
JavaScript Answers

How to add disabled attribute to input element using JavaScript?

Sometimes, we want to add disabled attribute to input element using JavaScript.

In this article, we’ll look at how to add disabled attribute to input element using JavaScript.

How to add disabled attribute to input element using JavaScript?

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.

For instance, we write

<select id="selectFrom">
  ...
</select>

to add the select element.

then we write

document.getElementById("selectFrom").disabled = true;

to get the select element with getElementById.

Then we set its disabled property to true to make it disabled.

Conclusion

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.