Categories
JavaScript Answers

How to generate random hex string in JavaScript?

Sometimes, we want to generate random hex string in JavaScript.

In this article, we’ll look at how to generate random hex string in JavaScript.

How to generate random hex string in JavaScript?

To generate random hex string in JavaScript, we can use the Math.random and toString methods.

For instance, we write:

const genRanHex = size => [...Array(size)]
  .map(() => Math.floor(Math.random() * 16).toString(16)).join('');

console.log(genRanHex(6));
console.log(genRanHex(8));

to define the genRanHex function to generate the hex number.

In the function, we create an array with the given size and make a copy of it with [...Array(size)].

Then we call map with a callback that returns the randomly generated hex number with Math.floor(Math.random() * 16).toString(16).

We call toString with 16 to convert the randomly generated decimal number to hex.

Finally, we call join with an empty string to combine them together.

Conclusion

To generate random hex string in JavaScript, we can use the Math.random and toString methods.

Categories
JavaScript Answers

How to convert a string to an XML Document in JavaScript?

Sometimes, we want to convert a string to an XML Document in JavaScript.

In this article, we’ll look at how to convert a string to an XML Document in JavaScript.

How to convert a string to an XML Document in JavaScript?

To convert a string to an XML Document in JavaScript, we can use the DOMParser constructor.

For instance, we write:

const xmlString = '<foo><bar>something</bar></foo>';
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
console.log(xmlDoc)

We create a new DOMParser instance.

And then we call parseFromString with the xmlString and the 'application/xml' MIME type to parse the string as XML.

Therefore, xmlDoc is an XML document converted from the string.

Conclusion

To convert a string to an XML Document in JavaScript, we can use the DOMParser constructor.

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.