Categories
JavaScript Answers

How to use array reduce with condition in JavaScript?

Sometimes, we want to use array reduce with condition in JavaScript.

In this article, we’ll look at how to use array reduce with condition in JavaScript.

How to use array reduce with condition in JavaScript?

To use array reduce with condition in JavaScript, we can use the array filter method to return an array with the filtered entries before we call reduce.

For instance, we write

records
  .filter(({ gender }) => gender === "GIRLS")
  .reduce((sum, record) => sum + record.value);

to call filter to return the items with the gender property set to 'GIRLS‘.

Then we call reduce to sum up the value property of each entry.

Conclusion

To use array reduce with condition in JavaScript, we can use the array filter method to return an array with the filtered entries before we call reduce.

Categories
Vue Answers

How to fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js?

Sometimes, we want to fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js.

In this article, we’ll look at how to fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js.

How to fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js?

To fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js, we should use a primitive value as the value of the key prop when we’re using v-for.

For instance, we write

<template>
  <div>
    ...
    <div v-for="comment in comments" :key="comment.id">...</div>
    ...
  </div>
</template>

to set the key prop to comment.id where comment.id is a unique ID string of each comments entry.

The key prop’s value helps Vue identify each item being rendered with v-for.

Conclusion

To fix the ‘Avoid using non-primitive value as key, use string/number value instead’ with Vue.js, we should use a primitive value as the value of the key prop when we’re using v-for.

Categories
Vue Answers

How to reverse the order of an array using v-for and orderBy filter in Vue.js?

Sometimes, we want to reverse the order of an array using v-for and orderBy filter in Vue.js.

In this article, we’ll look at how to reverse the order of an array using v-for and orderBy filter in Vue.js.

How to reverse the order of an array using v-for and orderBy filter in Vue.js?

The order of an array using v-for and orderBy filter in Vue.js, we can call the reverse method on the array.

For instance, we write

<template>
  <div>
    ...
    <li v-for="item in items.slice().reverse()">...</li>
    ...
  </div>
</template>

to make a copy of the items array with slice and then call reverse to reverse the cloned array.

And then we use v-for to render the items in the reversed array.

Conclusion

The order of an array using v-for and orderBy filter in Vue.js, we can call the reverse method on the array.

Categories
Vue Answers

How to make a component delete itself in Vue.js?

Sometimes, we want to make a component delete itself in Vue.js.

In this article, we’ll look at how to make a component delete itself in Vue.js.

How to make a component delete itself in Vue.js?

To make a component delete itself in Vue.js, we can use the this.$destroy method.

For instance, we write

<script>
//...
export default {
  //...
  methods: {
    close() {
      this.$destroy();
      this.$el.parentNode.removeChild(this.$el);
    },
  },
  //...
};
</script>

to add the close method to call this.$destroy to unmount the component.

And then we call this.$el.parentNode.removeChild(this.$el) to remove the current component’s root element from the DOM.

Conclusion

To make a component delete itself in Vue.js, we can use the this.$destroy method.

Categories
Vue Answers

How to set initial Vuetify v-select value with Vue.js?

Sometimes, we want to set initial Vuetify v-select value with Vue.js.

In this article, we’ll look at how to set initial Vuetify v-select value with Vue.js.

How to set initial Vuetify v-select value with Vue.js?

To set initial Vuetify v-select value with Vue.js, we set the value of the reactive property that’s bound to v-model to the value of the property that we set as the value of the item-value prop of the v-select.

For instance, we write

<template>
  <div>
    <v-select
      v-model="input.userId"
      :items="users"
      item-value="id"
      item-text="name"
      label="Users"
    />
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      input: {
        userId: 2,
      },
      users: [
        {
          id: 1,
          name: "John",
          last: "Doe",
        },
        {
          id: 2,
          name: "Harry",
          last: "Potter",
        },
        {
          id: 3,
          name: "Jane",
          last: "Smith",
        },
      ],
    };
  },
  //...
};
</script>

to set the input.userId reactive property to 2, which is value of the id property of the item in users that we want to be the default.

Therefore, ‘Harry Potter’ would be the default value since it has id 2 and item-value is set to id.

Conclusion

To set initial Vuetify v-select value with Vue.js, we set the value of the reactive property that’s bound to v-model to the value of the property that we set as the value of the item-value prop of the v-select.