Categories
JavaScript Answers

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

Sometimes, we want to fix Node.js Express.js app only working on port 3000 with JavaScript.

In this article, we’ll look at how to fix Node.js Express.js app only working on port 3000 with JavaScript.

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

For instance, we write

app.set("port", process.env.PORT || 3000);

to set the port of the Express app with app.set.

We try to get the port number from the PORT environment variable, which is process.env.PORT.

Otherwise, we use port 3000 as the fallback.

Then we start our Node Express app by running

PORT=8080 node app.js

to set the PORT environment variable to 8080 and run app.js which has the app.set code.

Conclusion

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

Categories
JavaScript Answers

How to force JavaScript to do math instead of putting two strings together?

Sometimes, we want to force JavaScript to do math instead of putting two strings together.

In this article, we’ll look at how to force JavaScript to do math instead of putting two strings together.

How to force JavaScript to do math instead of putting two strings together?

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.

For instance, we write

const x = +"11.5" + +"3.5";

to convert '11.5' and '3.5' to numbers with the unary + operator.

Then we add the returned numbers together and assign the sum to x.

Conclusion

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.

Categories
JavaScript Answers

How to remove all classes that begin with a certain string with JavaScript?

Sometimes, we want to remove all classes that begin with a certain string with JavaScript.

In this article, we’ll look at how to remove all classes that begin with a certain string with JavaScript.

How to remove all classes that begin with a certain string with JavaScript?

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

For instance, we write

const prefix = "prefix";
const classes = el.className.split(" ").filter((c) => {
  return !c.startsWith(prefix);
});
el.className = classes.join(" ").trim();

to call el.className.split to split the className string by the spaces into an array of substrings.

Then we call filter with a callback that filters out all entries that starts with prefix.

Next, we call join to join all the class name strings in the classes array back into a string separated by spaces.

Then we call trim to remove start and ending whitespaces.

Conclusions

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

Categories
JavaScript Answers

How to check if the array of objects have duplicate property values with JavaScript?

Sometimes, we want to check if the array of objects have duplicate property values with JavaScript.

In this article, we’ll look at how to check if the array of objects have duplicate property values with JavaScript.

How to check if the array of objects have duplicate property values with JavaScript?

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.

For instance, we write

const values = [
  { name: "someName1" },
  { name: "someName2" },
  { name: "someName3" },
  { name: "someName1" },
];

const uniqueValues = new Set(values.map((v) => v.name));

if (uniqueValues.size < values.length) {
  console.log("duplicates found");
}

to get the name property values of each values array entry with

values.map((v) => v.name)

Then we convert the array to a set with the Set constructor.

If the set’s size is less than valueslength, then we know there’re duplicates values in values.

Conclusion

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.

Categories
JavaScript Answers

How to merge two array of objects based on a key with JavaScript?

Sometimes, we want to merge two array of objects based on a key with JavaScript.

In this article, we’ll look at how to merge two array of objects based on a key with JavaScript.

How to merge two array of objects based on a key with JavaScript?

To merge two array of objects based on a key with JavaScript, we can use the array map and find methods and the spread operator.

For instance, we write

const arr1 = [
  { id: "abdc4051", date: "2017-01-24" },
  { id: "abdc4052", date: "2017-01-22" },
];

const arr2 = [
  { id: "abdc4051", name: "ab" },
  { id: "abdc4052", name: "abc" },
];

const mergeById = (a1, a2) =>
  a1.map((itm) => ({
    ...a2.find((item) => item.id === itm.id && item),
    ...itm,
  }));

console.log(mergeById(arr1, arr2));

to define the mergeById function.

In it, we call a1.map with a callback that has the contents of the object in a2 that has the same id of the current object in a1, which is itm and the itm object.

We find the object with the same id value as the one being looped through in a1 with

a2.find((item) => item.id === itm.id && item)

We spread the properties of each into a new object and return it.

Conclusion

To merge two array of objects based on a key with JavaScript, we can use the array map and find methods and the spread operator.