Categories
JavaScript Answers

How to Get Computed Styles of an Element with JavaScript?

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.

For instance, if we have:

<div>
  hello
</div>

Then we write:

const element = document.querySelector('div')
const style = window.getComputedStyle(element)
console.log(style)

We get the div with document.querySelector.

Then we call window.getComputedStyle with element as the argument.

Therefore, the console log should show all the computed CSS properties with their values for the div.

Categories
JavaScript Answers

How to Match a Whole Word in JavaScript?

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

For instance, we can write:

const lookup = '\n\n\n\n\n\nPC Games\n\n\n\n';
const word  = lookup.trim() ;
const match = new RegExp(`\\b${word}\\b`).test('2 PC Games')
console.log(match)

We create the word string with the words we’re looking for.

Then we create the regex with the RegExp constructor.

'\\b' is the word boundary for the pattern. This will let us match the whole word string.

Then we call test on the regex with the string we want to check if it matches the pattern.

Therefore, match should log true since the string matches the pattern.

Categories
JavaScript Answers

How to get the Month Name of Last Month using Moment.js?

To get the month name of last month using moment.js, we can use the subtract, startOf and format methods.

For instance, we can write:

const monthMinusOneName =  moment('2021-08-10').subtract(1, "month").startOf("month").format('MMMM');
console.log(monthMinusOneName)

We call moment to create moment date object.

Then we call subtract with 1 and 'month' to subtract 1 month from the moment date object.

Next, we call startOf with 'month' to return the moment date for the start of the subtracted date.

Finally, we call format with 'MMMM' to return the full month name.

Therefore, monthMinusOneName is 'July'.

Categories
JavaScript Answers

How to Remove an Object from an Array of Objects in JavaScript?

To remove an object from an array of objects in JavaScript, we can use the JavaScript array’s findIndex method to get the index of the first element that matches the given condition.

Then we can use the JavaScript array’s splice method to remove the item from the list.

For instance, we can write:

const arr = [{
  "value": "14",
  "label": "7"
}, {
  "value": "14",
  "label": "7"
}, {
  "value": "18",
  "label": "7"
}]


const matchesEl = (el) => {
  return el.value === '14' && el.label === '7';
}

arr.splice(arr.findIndex(matchesEl), 1);
console.log(arr)

We have the arr array with the object’s we want to remove.

Then we have the matchesEl fuinction that returns the boolean expression that matches an item where el.value is 14 and el.label is 7.

Next, we call arr.findIndex with matchesEl to return the index of the first object that has value 14 and label 7 in `arr.

Then we call arr.splice with the returned index and 1 to remove that entry from arr.

Therefore, arr is now:

[
  {
    "value": "14",
    "label": "7"
  },
  {
    "value": "18",
    "label": "7"
  }
]
Categories
JavaScript Answers

How to Extract the Text Out of HTML String Using JavaScript?

To extract the text out of HTML string using JavaScript, we can set the innerHTML property of an element to the HTML string.

Then we can use the textContent or the innerText property to get the text of the element.

For instance, we can write:

const extractContent = (s) => {
  const span = document.createElement('span');
  span.innerHTML = s;
  return span.textContent || span.innerText;
};

console.log(extractContent("<p>Hello</p><a href='http://example.org'>example</a>"))

to create the extractContent function that takes the s HTML string.

In the function, we call document.createElement to create the span element.

Then we set the innerHTML property of the span to s.

And finally, we return the textContent or innerText property to return the text content string.

Therefore, from the console log, we see 'Helloexample' logged.