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.

Categories
JavaScript Answers

How to Find the Last Matching Object in Array of Objects with JavaScript?

To set Letter spacing in canvas element with JavaScript, we can use the JavaScript array’s reverse and fine methods.

For instance, we can write:

const fruits = [{
    shape: 'round',
    name: 'orange'
  },
  {
    shape: 'round',
    name: 'apple'
  },
  {
    shape: 'oblong',
    name: 'zucchini'
  },
  {
    shape: 'oblong',
    name: 'banana'
  },
  {
    shape: 'round',
    name: 'grapefruit'
  }
]
const currentShape = 'round'
const fruit = fruits.slice().reverse().find(fruit => fruit.shape === currentShape);
console.log(fruit)

We have the fruits array.

And we want to find the last element that has the shape property set to 'round'.

To do this, we call fruits.slice to return a copy of the fruits array.

Then we call reverse to reverse the array copy.

And finally, we call find with a callback that checks if fruit.shape is equal to currentShape.

Therefore, fruit is:

{shape: "round", name: "grapefruit"}

as we expect.

Categories
JavaScript Answers

How to Set Letter Spacing in Canvas Element with JavaScript?

To set Letter spacing in canvas element with JavaScript, we can set the canvas element’s style.letterSpacing property.

For instance, we can write:

<canvas style='width: 300px; height: 300px'></canvas>

to add the canvas element.

Then, we write:

const can = document.querySelector('canvas');
const ctx = can.getContext('2d');
can.width = can.offsetWidth;
ctx.clearRect(0, 0, can.width, can.height);
can.style.letterSpacing = '10px'
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '4em sans-serif';
ctx.fillText('Hello', can.width / 2, can.height * 1 / 4);

can.style.letterSpacing = '30px'
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '4em sans-serif';
ctx.fillText('World', can.width / 2, can.height * 3 / 4);

to get the canvas with document.querySelector.

Then we get the canvas context with getContext.

Next, we set the can.style.letterSpacing to set the letter spacing we want for our text.

We also, set the textAlign, textBaseline, and font properties to set the font style we want.

Finally, we call fillText to draw the text onto the canvas.

The arguments are the text, width, and height of the text respectively.