Categories
JavaScript Answers

How to flatten a nested object with JavaScript?

To flatten a nested object with JavaScript, we use the Object.keys and array reduce method.

For instance, we write

const flattenObj = (obj = {}) => {
  return Object.keys(obj).reduce((acc, cur) => {
    if (typeof obj[cur] === "object") {
      acc = { ...acc, ...flattenObj(obj[cur]) };
    } else {
      acc[cur] = obj[cur];
    }
    return acc;
  }, {});
};

to call Object.keys with obj to get an array of keys.

Then we call reduce with a callback to recursively call crushObj when obj[cur] is an object.

And we put the properties in the acc object.

Otherwise, we put obj[cur] in acc.

We then return acc which is the flattened object.

Categories
JavaScript Answers

How to make a submit out of a link with JavaScript?

Sometimes, we want to make a submit out of a link with JavaScript.

In this article, we’ll look at how to make a submit out of a link with JavaScript.

How to make a submit out of a link with JavaScript?

To make a submit out of a link with JavaScript, we call submit on the form when we click on the link.

For instance, we write

<a href="#" onclick="document.forms['myFormName'].submit(); return false;">
  ...
</a>

to set onclick to the JavaScript code we run to submit the form to submit the form on click.

We use document.forms['myFormName'] to get the form with name or ID myFormName.

And we call submit to submit the form.

Then we return false to stop the default behavior of links.

Conclusion

To make a submit out of a link with JavaScript, we call submit on the form when we click on the link.

Categories
JavaScript Answers

How to only trigger parent click event when a child is clicked with JavaScript?

Sometimes, we want to only trigger parent click event when a child is clicked with JavaScript.

In this article, we’ll look at how to only trigger parent click event when a child is clicked with JavaScript.

How to only trigger parent click event when a child is clicked with JavaScript?

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation.

For instance, we write

parent.addEventListener(
  "click",
  (e) => {
    e.stopPropagation();
    console.log("event on parent!");
  },
  true
);

to call addEventListener to listen for clicks on the parent element.

In the event handler, we call stopPropagation to stop the event from going down to the child.

We call it with true to make events propagate down the DOM hierarchy instead of up.

Conclusion

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation.

Categories
JavaScript Answers

How to set max viewport in Puppeteer with JavaScript?

Sometimes, we want to set max viewport in Puppeteer with JavaScript.

In this article, we’ll look at how to set max viewport in Puppeteer with JavaScript.

How to set max viewport in Puppeteer with JavaScript?

To set max viewport in Puppeteer with JavaScript, we call puppeeter.launch with an object that sets the defaultViewport option.

For instance, we write

const browser = await puppeteer.launch({ defaultViewport: null });

to set defaultViewport to null to make Puppeteer open a window bigger than 800px x 600px.

Conclusion

To set max viewport in Puppeteer with JavaScript, we call puppeeter.launch with an object that sets the defaultViewport option.

Categories
JavaScript Answers

How to test if a variable does not equal either of two values with JavaScript?

Sometimes, we want to test if a variable does not equal either of two values with JavaScript.

In this article, we’ll look at how to test if a variable does not equal either of two values with JavaScript.

How to test if a variable does not equal either of two values with JavaScript?

To test if a variable does not equal either of two values with JavaScript, we use boolean operators.

For instance, we write

if (test !== "A" && test !== "B") {
  //...
}

to check if test isn’t 'A' and also isn’t 'B'.

Conclusion

To test if a variable does not equal either of two values with JavaScript, we use boolean operators.