Categories
JavaScript Answers

How to rank array elements with JavaScript?

Sometimes, we want to rank array elements with JavaScript.

In this article, we’ll look at how to rank array elements with JavaScript.

How to rank array elements with JavaScript?

To rank array elements with JavaScript, we can use the sort method.

For instance, we write:

const arr = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
const sorted = arr.slice().sort((a, b) => b - a)
const ranks = arr.map(v => sorted.indexOf(v) + 1);
console.log(ranks);

We call arr.slice to make a copy of arr.

Then we call sort on it to return a sorted version of arr sorted in descending order.

Then we call arr.map with a callback that returns the position of the sorted array the arr entry v is in plus 1.

Therefore, ranks is [2, 7, 4, 7, 3, 9, 5, 9, 1, 6].

Since 79 is 2nd largest, 5 is 7th largest etc.

Conclusion

To rank array elements with JavaScript, we can use the sort method.

Categories
JavaScript Answers

How to get an image natural height with JavaScript?

Sometimes, we want to get an image natural height with JavaScript.

In this article, we’ll look at how to get an image natural height with JavaScript.

How to get an image natural height with JavaScript?

To get an image natural height with JavaScript, we can use the naturalHeight property.

For instance, we write:

<img src='https://picsum.photos/200/300'>

to add an img element.

Then we write:

const image = document.querySelector('img')
console.log(image.naturalHeight)

to select the img element with querySelector.

And then we use the image.naturalHeight property to get the natural height of the image.

Conclusion

To get an image natural height with JavaScript, we can use the naturalHeight property.

Categories
JavaScript Answers

How to convert a JavaScript array of 2 element arrays into object key value pairs?

Sometimes, we want to convert a JavaScript array of 2 element arrays into object key value pairs.

In this article, we’ll look at how to convert a JavaScript array of 2 element arrays into object key value pairs.

How to convert a JavaScript array of 2 element arrays into object key value pairs?

To convert a JavaScript array of 2 element arrays into object key value pairs, we can use the Object.fromEntries method.

For instance, we write:

const array = [
  ['a', 1],
  ['b', 2],
  ['c', 3]
];
const object = Object.fromEntries(array);
console.log(object);

to call Object.fromEntries with an array of key-value pair arrays.

Therefore, object is {a: 1, b: 2, c: 3}.

Conclusion

To convert a JavaScript array of 2 element arrays into object key value pairs, we can use the Object.fromEntries method.

Categories
JavaScript Answers

How to submit form on enter key press with JavaScript?

Sometimes, we want to submit form on enter key press with JavaScript.

In this article, we’ll look at how to submit form on enter key press with JavaScript.

How to submit form on enter key press with JavaScript?

To submit form on enter key press with JavaScript, we can listen for the keydown event.

For instance, we write:

<form>
  <input>
</form>

to add a form.

Then we write:

const input = document.querySelector('input')
input.onkeydown = (e) => {
  if (e.keyCode === 13) {
    e.preventDefault()
    console.log('submit')
  }
};

to select the input with querySelector.

Then we set input.onkeydown to a function that checks if the key that’s pressed if the enter key.

We check that by checking if e.keyCode is 13.

Conclusion

To submit form on enter key press with JavaScript, we can listen for the keydown event.

Categories
JavaScript Answers

How to remove the end of a string starting from a given pattern with JavaScript?

Sometimes, we want to remove the end of a string starting from a given pattern with JavaScript.

In this article, we’ll look at how to remove the end of a string starting from a given pattern with JavaScript.

How to remove the end of a string starting from a given pattern with JavaScript?

To remove the end of a string starting from a given pattern with JavaScript, we can use the string’s substring method.

For instance, we write:

const str = "/abcd/efgh/ijkl/xxx-1/xxx-2";
const s = str.substring(0, str.indexOf("xxx"));
console.log(s)

to call str.string with the start and end indexes of str we want to return.

We have str.indexOf("xxx") to get the index of the first character of the first instance of 'xxx'.

And then we call str.substring with the returned index as the end index.

The character at the end index itself is excluded from the string returned by substring.

Therefore, s is '/abcd/efgh/ijkl/'.

Conclusion

To remove the end of a string starting from a given pattern with JavaScript, we can use the string’s substring method.