Categories
JavaScript Answers

How to map over object preserving keys with JavaScript?

Sometimes, we want to map over object preserving keys with JavaScript.

In this article, we’ll look at how to map over object preserving keys with JavaScript.

How to map over object preserving keys with JavaScript?

To map over object preserving keys with JavaScript, we can use the Object.fromEntries and Object.entries methods.

For instance, we write

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

to call Object.entries with obj to return an array of key-value pair arrays.

Then we call map to map each entry to an array with key k and new value v * 3, where v is the original property value.

Next, we call Object.fromEntries to convert the nested array back to an object.

Conclusion

To map over object preserving keys with JavaScript, we can use the Object.fromEntries and Object.entries methods.

Categories
JavaScript Answers

How to find index of all occurrences of element in array with JavaScript?

Sometimes, we want to find index of all occurrences of element in array with JavaScript.

In this article, we’ll look at how to find index of all occurrences of element in array with JavaScript.

How to find index of all occurrences of element in array with JavaScript?

To find index of all occurrences of element in array with JavaScript, we can use the array map method.

For instance, we write

const indices = array.map((e, i) => (e === value ? i : "")).filter(String);

to call array.map to return all the indices of all the items in array that’s equal to value.

If entry e equals value, then we return the index i of it.

Then we call filter with string to filter out any non-number entries.

Conclusion

To find index of all occurrences of element in array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to obfuscate an e-mail address on a website with JavaScript?

Sometimes, we want to obfuscate an e-mail address on a website with JavaScript.

In this article, we’ll look at how to obfuscate an e-mail address on a website with JavaScript.

How to obfuscate an e-mail address on a website with JavaScript?

To obfuscate an e-mail address on a website with JavaScript, we can use the btoa function.

For instance, we write

const obfuscatedEmail = btoa("mailto:email@example.com");

to call btoa with the email URL string to base64 encode the string.

This will obfuscate the string from users.

Then we can call atob with the obfuscated string to decode it back to the original URL.

Conclusion

To obfuscate an e-mail address on a website with JavaScript, we can use the btoa function.

Categories
JavaScript Answers

How to fix React SyntheticEvent stopPropagation() only working with React events?

Sometimes, we want to fix React SyntheticEvent stopPropagation() only working with React events.

In this article, we’ll look at how to fix React SyntheticEvent stopPropagation() only working with React events.

How to fix React SyntheticEvent stopPropagation() only working with React events?

To fix React SyntheticEvent stopPropagation() only working with React events, we can use the ev.nativeEvent.stopImmediatePropagation method to stop native events propagation.

For instance, we write

ev.stopPropagation();
ev.nativeEvent.stopImmediatePropagation();

in a event handler function in our React component to stop the propagation of React synthetic events and native events.

We use ev.stopPropagation(); to stop the propagation of React synthetic events.

And we use ev.nativeEvent.stopImmediatePropagation() to stop the propagation of native events.

Conclusion

To fix React SyntheticEvent stopPropagation() only working with React events, we can use the ev.nativeEvent.stopImmediatePropagation method to stop native events propagation.

Categories
JavaScript Answers

How to check if event is triggered by a human with JavaScript?

Sometimes, we want to check if event is triggered by a human with JavaScript.

In this article, we’ll look at how to check if event is triggered by a human with JavaScript.

How to check if event is triggered by a human with JavaScript?

To check if event is triggered by a human with JavaScript, we can use the isTrusted property.

For instance, we write

if (e.isTrusted) {
  // ...
} else {
  // ...
}

to check if the event is triggered by a human in our event handler callback.

e is the event object and it had the isTrusted property.

If isTrusted is true, then the event is triggered by a human.

Conclusion

To check if event is triggered by a human with JavaScript, we can use the isTrusted property.