Categories
JavaScript Answers

How to turn all the keys of an object to lower case with JavaScript?

To turn all the keys of an object to lower case with JavaScript, we use some object methods.

For instance, we write

const newObj = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v])
);

to call Object.entries to return an array of key-valye pair arrays from the obj.

Then we call map with a callback to map the keys to lower case with toLowwerCase and v.

And then we call Object.fromEntries to convert the mapped key-value pair array back to an object.

Categories
JavaScript Answers

How to ignore incompatible engine “node” error on installing npm dependencies with yarn?

To ignore incompatible engine "node" error on installing npm dependencies with yarn, we remove the engines entry from the package.json file.

We can also run

yarn config set ignore-engines true

to ignore the engines entry in package.json once by setting the ignore-engines setting to true.

Categories
JavaScript Answers

How to remove all properties from a object with Node.js?

To remove all properties from a object with Node.js, we assign the variable to an empty object.

For instance, we write

req.session = {};

to assign req.session to an empty object to remove all properties from it.

Categories
JavaScript Answers

How to fix “Error: Cannot find module ‘ejs’ ” with Node.js?

To fix "Error: Cannot find module ‘ejs’ " with Node.js, we install the ejs library.

To install it, we run

npm install ejs

in our project folder to install the ejs package with npm.

Categories
JavaScript Answers

How to define an array as an environment variable in Node.js?

To define an array as an environment variable in Node.js, we can define it as a JSON array.

For instance, we write

ANY_LIST = ["A", "B", "C"]

to add the ANY_LIST environment variable to our .env file.

Then we write

const LIST = JSON.parse(process.env.ANY_LIST);

to get the ANY_LIST environment variable with process.env.ANY_LIST.

And we parse it into an array with JSON.parse.