Categories
JavaScript Answers

How to remove item from array using its name / value with JavaScript?

Sometimes, we want to remove item from array using its name / value with JavaScript.

In this article, we’ll look at how to remove item from array using its name / value with JavaScript.

How to remove item from array using its name / value with JavaScript?

To remove item from array using its name / value with JavaScript, we can use the JavaScript array’s findIndex and splice methods.

For instance, we write:

const countries = [{
    id: 'AF',
    name: 'Afghanistan'
  },
  {
    id: 'AL',
    name: 'Albania'
  },
  {
    id: 'DZ',
    name: 'Algeria'
  }
];

const index = countries.findIndex(c => c.id === 'AF')
countries.splice(index, 1)
console.log(countries)

We call countries.findIndex with a callback that finds the entry with id set to 'AF'.

Then we call splice with the index found and 1 to remove the entry with the given index.

Therefore, countries is now:

[
  {
    "id": "AL",
    "name": "Albania"
  },
  {
    "id": "DZ",
    "name": "Algeria"
  }
]

according to the console log.

Conclusion

To remove item from array using its name / value with JavaScript, we can use the JavaScript array’s findIndex and splice methods.

Categories
JavaScript Answers

How to create an array of integers from 1 to 20 in JavaScript?

Sometimes, we want to create an array of integers from 1 to 20 in JavaScript.

In this article, we’ll look at how to create an array of integers from 1 to 20 in JavaScript.

How to create an array of integers from 1 to 20 in JavaScript?

To create an array of integers from 1 to 20 in JavaScript, we can use the JavaScript array instance’s fill and map methods.

For instance, we write:

const arr = Array(20).fill().map((x, i) => i + 1)
console.log(arr)

We can Array with 20 to create an array with 20 empty slots.

Then we call fill to fill the empty slots with undefined.

Finally, we call map with a callback to return the numbers from 1 to 20.

Therefore, arr is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20].

Conclusion

To create an array of integers from 1 to 20 in JavaScript, we can use the JavaScript array instance’s fill and map methods.

Categories
JavaScript Answers

How to adjust the height of an element to the screen’s height with jQuery?

Sometimes, we want to adjust the height of an element to the screen’s height with jQuery.

In this article, we’ll look at how to adjust the height of an element to the screen’s height with jQuery.

How to adjust the height of an element to the screen’s height with jQuery?

To adjust the height of an element to the screen’s height with jQuery, we can call the css method.

For instance, we write:

<div style='background-color: green'>

</div>

to add a div.

Then we write:

const div = $('div');
div.css("height", `${$(window).height()}px`);

We select the div with $.

Then we call div.css with 'height' and the height of window which we get with $(window).height().

We’ve to add the unit at the end to set the height.

Conclusion

To adjust the height of an element to the screen’s height with jQuery, we can call the css method.

Categories
JavaScript Answers

How to use jQuery to check whether at least one checkbox is checked?

Sometimes, we want to use jQuery to check whether at least one checkbox is checked.

In this article, we’ll look at how to use jQuery to check whether at least one checkbox is checked.

How to use jQuery to check whether at least one checkbox is checked?

To use jQuery to check whether at least one checkbox is checked, we can selected the checked checkboxes and get the length of that.

For instance, we write:

<form>
  <input type="checkbox" value="true" checked="true" name="chk[120]">
  <input type="checkbox" value="true" checked="true" name="chk[128]">
  <input type="checkbox" name="chk[130]">
  <input type="checkbox" name="chk[143]">
  <input type="submit" name="btnsubmit" value="Submit">
</form>

to add a form with some checkboxes.

Then we write:

const someChecked = $('form input:checked').length > 0
console.log(someChecked)

We select the checked checkboxes with $('form input:checked').

And we use length to get the number of checked checkboxes selected.

Therefore, someChecked should be true since some checkboxes are checked.

Conclusion

To use jQuery to check whether at least one checkbox is checked, we can selected the checked checkboxes and get the length of that.

Categories
JavaScript Answers

How to get the first element rather than using [0] in jQuery?

Sometimes, we want to get the first element rather than using [0] in jQuery.

In this article, we’ll look at how to get the first element rather than using [0] in jQuery.

How to get the first element rather than using [0] in jQuery?

To get the first element rather than using [0] in jQuery, we can use the get method.

For instance, we write:

<div>
  foo
</div>
<div>
  bar
</div>
<div>
  baz
</div>

to add a few divs.

Then we write:

const el = $('div').get(0) 
console.log(el)

to select all the divs with $.

Then we call get with 0 to return the first one selected.

Therefore, el is the div with text ‘foo’.

Conclusion

To get the first element rather than using [0] in jQuery, we can use the get method.