Categories
JavaScript Answers

How to remove duplicates form an array with JavaScript?

Sometimes, we want to remove duplicates form an array with JavaScript.

In this article, we’ll look at how to remove duplicates form an array with JavaScript.

How to remove duplicates form an array with JavaScript?

To remove duplicates form an array with JavaScript, we can use the sets and the array map method.

For instance, we write

const array = [
  { id: 123, value: "value1", name: "Name1" },
  { id: 124, value: "value2", name: "Name1" },
  { id: 125, value: "value3", name: "Name2" },
  { id: 126, value: "value4", name: "Name2" },
];
const names = [...new Set(array.map((a) => a.name))];

console.log(names);

to call array.map with a callback to map each array entry to the name property of each entry.

Then we create a new set with the mapped array with the Set constructor.

Finally, we use the spread operator to spread it back to an array.

Conclusion

To remove duplicates form an array with JavaScript, we can use the sets and the array map method.

Categories
JavaScript Answers

How to dump object with JavaScript?

Sometimes, we want to dump object with JavaScript.

In this article, we’ll look at how to dump object with JavaScript.

How to dump object with JavaScript?

To dump object with JavaScript, we can use the console.log method.

For instance, we write

console.log("my object: %o", myObj);

to call console.log with a string with the '%o' placeholder and the myObj object to dump the content of the myObj object onto the console.

Conclusion

To dump object with JavaScript, we can use the console.log method.

Categories
JavaScript Answers

How to detect blocked popup in Chrome with JavaScript?

Sometimes, we want to detect blocked popup in Chrome with JavaScript.

In this article, we’ll look at how to detect blocked popup in Chrome with JavaScript.

How to detect blocked popup in Chrome with JavaScript?

To detect blocked popup in Chrome with JavaScript, we can check if the popup object is defined or if its screenX property is 0.

For instance, we write

const myPopup = window.open(url, "screenX=100");
if (!myPopup) {
  alert("failed for most browsers");
} else {
  myPopup.onload = () => {
    setTimeout(() => {
      if (myPopup.screenX === 0) {
        alert("failed for chrome");
      }
    }, 0);
  };
}

to call window.open to open a popup that opens loads url.

Then we check if myPopup is defined.

If it’s not, then it didn’t open.

Otherwise, we check in the myPopup.onload method if the screenX property of myPopup is 0.

If it is, then the popup also didn’t load.

Conclusion

To detect blocked popup in Chrome with JavaScript, we can check if the popup object is defined or if its screenX property is 0.

Categories
JavaScript Answers

How to make a HTML button perform a POST request?

Sometimes, we want to make a HTML button perform a POST request.

In this article, we’ll look at how to make a HTML button perform a POST request.

How to make a HTML button perform a POST request?

To make a HTML button perform a POST request, we add a name attribute to the button.

For instance, we write

<form action="" method="post">
  <button name="foo" value="upvote">Upvote</button>
</form>

to set the name attribute of the button to foo.

Then when we press it, it’ll perform a post request.

We set the method attribute to post so that it performs the post request when we click it.

Conclusion

To make a HTML button perform a POST request, we add a name attribute to the button.

Categories
JavaScript Answers

How to properly return multiple values from a promise with JavaScript?

Sometimes, we want to properly return multiple values from a promise with JavaScript.

In this article, we’ll look at how to properly return multiple values from a promise with JavaScript.

How to properly return multiple values from a promise with JavaScript?

To properly return multiple values from a promise with JavaScript, we can return a promise that resolves to an array.

For instance, we write

const step1 = () => {
  const server = "myserver.com";
  const data = "so much data";
  return Promise.resolve([server, data]);
};

const f = async () => {
  const [server, data] = await step1();
  //...
};

to create the step1 function.

In it, we call Promise.resolve to create a promise that resolves to an array of data.

Then in function f, we use await to get the resolve value from the promise returned by step1.

And then we destructure the values from the array.

Conclusion

To properly return multiple values from a promise with JavaScript, we can return a promise that resolves to an array.