Categories
JavaScript Answers

How to split a JavaScript string into fixed-length pieces?

Sometimes, we want to split a JavaScript string into fixed-length pieces.

In this article, we’ll look at how to split a JavaScript string into fixed-length pieces.

How to split a JavaScript string into fixed-length pieces?

To split a JavaScript string into fixed-length pieces, we can call the string’s match method with a regex.

For instance, we write:

const a = 'aaaabbbbccccee';
const b = a.match(/(.{1,4})/g);
console.log(b)

to call a.match with a regex that matches characters in groups of 4 throughout the string.

As a result, we get that b is ['aaaa', 'bbbb', 'cccc', 'ee'].

Conclusion

To split a JavaScript string into fixed-length pieces, we can call the string’s match method with a regex.

Categories
JavaScript Answers

How to move element to first position in array with JavaScript?

Sometimes, we want to move element to first position in array with JavaScript.

In this article, we’ll look at how to move element to first position in array with JavaScript.

How to move element to first position in array with JavaScript?

To move element to first position in array with JavaScript, we can use the spread operator and the array slice method.

For instance, we write:

const data = [{
    id: 1,
    age: 24,
    gender: "male"
  },
  {
    id: 2,
    age: 27,
    gender: "female"
  },
  {
    id: 3,
    age: 30,
    gender: "male"
  },
  {
    id: 4,
    age: 10,
    gender: "female"
  },
]
const newData = [...data.slice(3), ...data.slice(0, 3)]
console.log(newData)

We call data.slice to get the last element and the first 3 elements respectively.

Then we spread each array into a new array to populate the values of the returned arrays in the new array.

As a result, we get:

[
  {
    "id": 4,
    "age": 10,
    "gender": "female"
  },
  {
    "id": 1,
    "age": 24,
    "gender": "male"
  },
  {
    "id": 2,
    "age": 27,
    "gender": "female"
  },
  {
    "id": 3,
    "age": 30,
    "gender": "male"
  }
]

for newData.

Conclusion

To move element to first position in array with JavaScript, we can use the spread operator and the array slice method.

Categories
JavaScript Answers

How to convert lowercase letter to upper case with JavaScript?

Sometimes, we want to convert lowercase letter to upper case with JavaScript.

In this article, we’ll look at how to convert lowercase letter to upper case with JavaScript.

How to convert lowercase letter to upper case with JavaScript?

To convert lowercase letter to upper case with JavaScript, we can use the string’s toUpperCase method.

For instance, we write:

const str = 'abc'
const newStr = str.toUpperCase();
console.log(newStr)

to call str.toUpperCase to return a string with characters in str converted to upper case.

Therefore, newStr is 'ABC'.

Conclusion

To convert lowercase letter to upper case with JavaScript, we can use the string’s toUpperCase method.

Categories
JavaScript Answers

How to add an HTML file input that only allows a user to select only one file?

Sometimes, we want to add an HTML file input that only allows a user to select only one file.

In this article, we’ll look at how to add an HTML file input that only allows a user to select only one file.

How to add an HTML file input that only allows a user to select only one file?

To add an HTML file input that only allows a user to select only one file, we can add a file input without the multiple attribute.

For instance, we write:

<input type="file" id="file" name="file" />

to add a file input that only allows us to select a single file.

Conclusion

To add an HTML file input that only allows a user to select only one file, we can add a file input without the multiple attribute.

Categories
JavaScript Answers

How to escape HTML with JavaScript?

Sometimes, we want to escape HTML with JavaScript.

In this article, we’ll look at how to escape HTML with JavaScript.

How to escape HTML with JavaScript?

To escape HTML with JavaScript, we can put the HTML string in an element.

Then we can get the escaped string from the innerHTML property.

For instance, we write:

const escapeHTML = (str) => {
  const p = document.createElement("p");
  p.appendChild(document.createTextNode(str));
  return p.innerHTML;
}
const escaped = escapeHTML('Test & Sample')
console.log(escaped)

to define the escapeHTML function that takes the str string as a parameter.

In it, we create a p element with createElement.

Then we call appendChild with the text node that we create from str with createTextNode.

Finally, we return the innerHTML property to return the escaped string.

Therefore, escaped is 'Test &amp; Sample'.

Conclusion

To escape HTML with JavaScript, we can put the HTML string in an element.

Then we can get the escaped string from the innerHTML property.