Categories
JavaScript Answers Vue Answers

How to get coordinates of a mouse click in Vue.js and JavaScript?

Sometimes, we want to get coordinates of a mouse click in Vue.js and JavaScript.

In this article, we’ll look at how to get coordinates of a mouse click in Vue.js and JavaScript.

How to get coordinates of a mouse click in Vue.js and JavaScript?

To get coordinates of a mouse click in Vue.js and JavaScript, we can use get the properties of the mouse click event object.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div @click='onClick'>
    	click me
    </div>
  `,
  methods: {
    onClick(e) {
      console.log(event.clientX);
      console.log(event.clientY);
      
      console.log(event.pageX);
      console.log(event.pageY);
      
      console.log(event.screenX);
      console.log(event.screenY);
    }
  }
})

to add a div with a click handler.

When we click the div, onClick is run.

Then from the e parameter of onClick, we get the position of the click in various ways.

clientX and clientY give the coordinates of the mouse relative to the viewport in CSS pixels.

pageX and pageY give the coordinates of the mouse relative to the <html> element in CSS pixels.

screenX and screenY give the coordinates of the mouse relative to the screen in device pixels.

Conclusion

To get coordinates of a mouse click in Vue.js and JavaScript, we can use get the properties of the mouse click event object.

Categories
JavaScript Answers

How to split a string at last occurrence of character then join with JavaScript?

Sometimes, we want to split a string at last occurrence of character then join with JavaScript.

In this article, we’ll look at how to split a string at last occurrence of character then join with JavaScript.

How to split a string at last occurrence of character then join with JavaScript?

To split a string at last occurrence of character then join with JavaScript, we can use some string methods.

For instance, we write:

const k = "ext.abc.jpg";
const l = `${k.substring(0, k.lastIndexOf("."))}-fx${k.substring(k.lastIndexOf("."))}`
console.log(l);

to call the substring method to get the substring of k between the first character and the index of the last occurrence of . exclusive.

Then we call substring with the last index of . to the end of the string.

And then we combine them together in a template literal.

We used lastIndexOf to get the index of the last ..

Therefore, l is 'ext.abc-fx.jpg'.

Conclusion

To split a string at last occurrence of character then join with JavaScript, we can use some string methods.

Categories
JavaScript Answers

How to filter an array of objects by array of IDs with JavaScript?

Sometimes, we want to filter an array of objects by array of IDs with JavaScript.

In this article, we’ll look at how to filter an array of objects by array of IDs with JavaScript.

How to filter an array of objects by array of IDs with JavaScript?

To filter an array of objects by array of IDs with JavaScript, we can use the JavaScript array’s filter method.

For instance, we write:

const activeIds = [202, 204]
const serviceList = [{
    "id": 201,
    "title": "a"
  },
  {
    "id": 202,
    "title": "a"
  },
  {
    "id": 203,
    "title": "c"
  },
  {
    "id": 204,
    "title": "d"
  },
  {
    "id": 205,
    "title": "e"
  }
];

const activeServiceList = serviceList.filter((item) => {
  return activeIds.includes(item.id);
});
console.log(activeServiceList);

to call serviceList.filter with a callback that returns the activeIds.includes(item.id).

We call activeIds.includes to check if item.id is in the activeIds array.

This will return the serviceList entries with the id value that is includes in the activeIds array.

Therefore, we get:

[
  {
    "id": 202,
    "title": "a"
  },
  {
    "id": 204,
    "title": "d"
  }
]

logged.

How to filter an array of objects by array of IDs with JavaScript?

To filter an array of objects by array of IDs with JavaScript, we can use the JavaScript array’s filter method.

Categories
JavaScript Answers

How to create a JavaScript associative array with negative integer keys?

Sometimes, we want to create a JavaScript associative array with negative integer keys.

In this article, we’ll look at how to create a JavaScript associative array with negative integer keys.

How to create a JavaScript associative array with negative integer keys?

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.

For instance, we write:

const o = {
  '-1': 'apple',
  '-2': 'orange'
}
console.log(o)

to define object o which has negative integers as keys.

Therefore, o is {-1: 'apple', -2: 'orange'} according to the console log.

Conclusion

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.

Categories
JavaScript Answers

How to return elements where ID begins with a certain string with JavaScript?

Sometimes, we want to return elements where ID begins with a certain string with JavaScript.

In this article, we’ll look at how to return elements where ID begins with a certain string with JavaScript.

How to return elements where ID begins with a certain string with JavaScript?

To return elements where ID begins with a certain string with JavaScript, we can select elements with querySelectorAll.

Then we can convert the returned node list into an array and use filter to return an array of elements with the given ID pattern.

For instance, we write:

<div id='foo1'>
  1
</div>
<div id='foo2'>
  2
</div>
<div id='bar'>
  3
</div>

to add divs with IDs.

Then we write:

const divs = document.querySelectorAll('div')
const fooDivs = [...divs]
  .filter(d => d.id.includes('foo'))
console.log(fooDivs)

We select all the divs with querySelectorAll.

Then we spread the divs into an array.

And then we call filter to return an array with the ones with ids that includes 'foo'.

Therefore, fooDivs should have the first 2 divs.

Conclusion

To return elements where ID begins with a certain string with JavaScript, we can select elements with querySelectorAll.

Then we can convert the returned node list into an array and use filter to return an array of elements with the given ID pattern.