Categories
JavaScript Answers

How to run where statement with date with Sequelize?

Sometimes, we want to run where statement with date with Sequelize.

In this article, we’ll look at how to run where statement with date with Sequelize.

How to run where statement with date with Sequelize?

To run where statement with date with Sequelize, we use Op properties to form the query.

For instance, we write

const { Op } = require("sequelize");

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, "days").toDate(),
    },
  },
});

to call findAll with an object to query for entries with start_datetime bigger than or equal to 7 days ago.

Op.gte is greater than or equal to.

Conclusion

To run where statement with date with Sequelize, we use Op properties to form the query.

Categories
JavaScript Answers

How to properly unload or destroy a video element with JavaScript?

Sometimes, we want to properly unload or destroy a video element with JavaScript.

In this article, we’ll look at how to properly unload or destroy a video element with JavaScript.

How to properly unload or destroy a video element with JavaScript?

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.

For instance, we write

const videoElement = document.getElementById("id_of_the_video_element_here");
videoElement.pause();
videoElement.removeAttribute("src");
videoElement.load();

to get the video element with getElementById.

Then we call removeAttribute to remove the src attribute from the video element to unload it.

Conclusion

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.

Categories
JavaScript Answers

How to wrap content in a div with JavaScript?

Sometimes, we want to wrap content in a div with JavaScript.

In this article, we’ll look at how to wrap content in a div with JavaScript.

How to wrap content in a div with JavaScript?

To wrap content in a div with JavaScript, we can call the appendChild method.

For instance, we write

const wrap = (toWrap, wrapper) => {
  const newWrapper = wrapper ?? document.createElement("div");
  toWrap.parentNode.appendChild(newWrapper);
  return newWrapper.appendChild(toWrap);
};

to define the wrap function.

In it, we get the wrapperor create a new div withcreateElement` if it’s not set.

Then we call toWrap.parentNode.appendChild to append the newWrapper as the child of the parent node of the toWrap element.

And then we use newWrapper.appendChild(toWrap) to append toWrap as the last child of newWrapper.

Conclusion

To wrap content in a div with JavaScript, we can call the appendChild method.

Categories
JavaScript Answers

How to use promise function inside JavaScript array map?

Sometimes, we want to use promise function inside JavaScript array map.

In this article, we’ll look at how to use promise function inside JavaScript array map.

How to use promise function inside JavaScript array map?

To use promise function inside JavaScript array map, we use the Promise.all function.

For instance, we write

const mappedArray = await Promise.all(
  array.map(async (p) => {
    const i = await getPromise(p);
    return i.item;
  })
);

to call array.map with an async function that returns a promise with i.item as its resolve value.

map returns an array of promises with their resolve values.

Then we use Promise.all to run all the promises and get their resolve values with await.

Conclusion

To use promise function inside JavaScript array map, we use the Promise.all function.

Categories
Vue Answers

How to add a Vue Vuex getter with argument?

Sometimes, we want to add a Vue Vuex getter with argument.

In this article, we’ll look at how to add a Vue Vuex getter with argument.

How to add a Vue Vuex getter with argument?

To add a Vue Vuex getter with argument, we can add a getter function that returns a function that takes the arguments.

For instance, we write

new Vuex.Store({
  getters: {
    someMethod: (state) => (id) => {
      return state.things.find((thing) => thing.id === id);
    },
  },
});

to create a Vuex store with the someMethod getter that returns a function that takes the id parameter.

Then we call someMethod with the id we want.

Conclusion

To add a Vue Vuex getter with argument, we can add a getter function that returns a function that takes the arguments.