Categories
JavaScript Answers

How to get the unique elements in a JavaScript array and sort them?

Sometimes, we want to get the unique elements in a JavaScript array and sort them.

In this article, we’ll look at how to get the unique elements in a JavaScript array and sort them.

How to get the unique elements in a JavaScript array and sort them?

To get the unique elements in a JavaScript array and sort them, we use sets and the array sort method.

For instance, we write

const myData = ["237", "124", "255", "124", "366", "255"];
const uniqueAndSorted = [...new Set(myData)].sort();

to convert the myData array to a set with the Set constructor to remove the duplicate entries.

Then we convert the set back to an array with the spread operator.

Finally, we call sort to return a sorted version of the array.

Conclusion

To get the unique elements in a JavaScript array and sort them, we use sets and the array sort method.

Categories
JavaScript Answers

How to load JSON without a http get with d3 and JavaScript?

Sometimes, we want to load JSON without a http get with d3 and JavaScript.

In this article, we’ll look at how to load JSON without a http get with d3 and JavaScript.

How to load JSON without a http get with d3 and JavaScript?

To load JSON without a http get with d3 and JavaScript, we use the native JSON.parse method.

For instance, we write

const json = JSON.parse(myJson);

to call JSON.parse with the myJson json string to parse the string into a JavaScript object.

Conclusion

To load JSON without a http get with d3 and JavaScript, we use the native JSON.parse method.

Categories
JavaScript Answers

How to add a listener which is called if less than 2 options on a select element with JavaScript?

Sometimes, we want to add a listener which is called if less than 2 options on a select element with JavaScript.

In this article, we’ll look at how to add a listener which is called if less than 2 options on a select element with JavaScript.

How to add a listener which is called if less than 2 options on a select element with JavaScript?

To add a listener which is called if less than 2 options on a select element with JavaScript, we listen to the click event.

For instance, we write

<select id="activitySelector">
  <option value="addNew">Add New Item</option>
</select>

to add the select element.

Then we write

const activities = document.getElementById("activitySelector");

const addActivityItem = () => {
  //...
};

activities.addEventListener("click", () => {
  const options = activities.querySelectorAll("option");
  const count = options.length;
  if (typeof count === "undefined" || count < 2) {
    addActivityItem();
  }
});

to select the select element.

And then we call addEventListener to add a click event listener to the drop down.

In it, we check if there’re less than 2 options with

const options = activities.querySelectorAll("option");
const count = options.length;

and count < 2.

We get all the options in the drop down with activities.querySelectorAll("option").

If count is undefined or if it’s less than 2, then we call the addActivityItem function.

Conclusion

To add a listener which is called if less than 2 options on a select element with JavaScript, we listen to the click event.

Categories
JavaScript Answers

How to return a variable from the child promise with JavaScript?

Sometimes, we want to return a variable from the child promise with JavaScript.

In this article, we’ll look at how to return a variable from the child promise with JavaScript.

How to return a variable from the child promise with JavaScript?

To return a variable from the child promise with JavaScript, we use async and await.

For instance, we write

const top = async () => {
  const parent = await ParentPromise();
  const child = await ChildPromise(parent.id);
  return child.result.items;
};

to define the top function.

In it, we get the result of the ParentPromise promise with await.

And then we get the result of the ChildPromise promise with await.

Finally, we return the child.result.items property.

We can then use

const items = await top();

to get the value in another async function.

Conclusion

To return a variable from the child promise with JavaScript, we use async and await.

Categories
Vue Answers

How to trigger events using Vue.js?

Sometimes, we want to trigger events using Vue.js.

In this article, we’ll look at how to trigger events using Vue.js.

How to trigger events using Vue.js?

To trigger events using Vue.js, we can use refs.

For instance, we write

<template>
  <button type="button" @click="myClickEvent" ref="myBtn">Click Me!</button>
</template>

<script>
export default {
  methods: {
    myClickEvent() {
      const elem = this.$refs.myBtn;
      elem.click();
    },
  },
};
</script>

to assign the myBtn ref to the button.

Then in the myClickEvent method, we get the button with this.$refs.myBtn.

And then we call click on it to click it programmatically.

Conclusion

To trigger events using Vue.js, we can use refs.