Categories
JavaScript Answers

How to check if a string is entirely made of the same substring with JavaScript?

Sometimes, we want to check if a string is entirely made of the same substring with JavaScript.

In this article, we’ll look at how to check if a string is entirely made of the same substring with JavaScript.

How to check if a string is entirely made of the same substring with JavaScript?

To check if a string is entirely made of the same substring with JavaScript, we can use the indexOf method with a string that concatenated with itself.

For instance, we write

const check = (str) => {
  return (str + str).indexOf(str, 1) !== str.length;
};

to check if the start index of str in str + str when searching from index 1 isn’t str.length.

If this is true, then str isn’t entirely made of the same substring as itself.

Conclusion

To check if a string is entirely made of the same substring with JavaScript, we can use the indexOf method with a string that concatenated with itself.

Categories
JavaScript Answers

How to do deep clone in JavaScript?

Sometimes, we want to do deep clone in JavaScript.

In this article, we’ll look at how to do deep clone in JavaScript.

How to do deep clone in JavaScript?

To do deep clone in JavaScript, we use the structuredClone function.

For instance, we write

const original = { name: "example" };

const clone = structuredClone(original);

to call structuredClone with the original object to return a deep clone of the original object.

structuredClone is built into modern browsers.

Conclusion

To do deep clone in JavaScript, we use the structuredClone function.

Categories
JavaScript Answers

How to concatenate two numbers in JavaScript?

Sometimes, we want to concatenate two numbers in JavaScript.

In this article, we’ll look at how to concatenate two numbers in JavaScript.

How to concatenate two numbers in JavaScript?

To concatenate two numbers in JavaScript, we can use template literals.

For instance, we write

const a = 5;
const b = 6;
const numbersAsString = `${a}${b}`;

console.log(numbersAsString);

to interpolate the values a and b into the numersAsString template literal.

Then numbersAsString is '56'.

Conclusion

To concatenate two numbers in JavaScript, we can use template literals.

Categories
JavaScript Answers

How to add keys for grouped output with lodash .groupBy and JavaScript?

Sometimes, we want to add keys for grouped output with lodash .groupBy and JavaScript.

In this article, we’ll look at how to add keys for grouped output with lodash .groupBy and JavaScript.

How to add keys for grouped output with lodash .groupBy and JavaScript?

To add keys for grouped output with lodash .groupBy and JavaScript, we can use the map method to add the keys returned by the groupby method.

For instance, we write

const data = [
  {
    name: "jim",
    color: "blue",
    age: "22",
  },
  {
    name: "non",
    color: "blue",
    age: "33",
  },
  {
    name: "joey",
    color: "green",
    age: "77",
  },
];

console.log(
  _.chain(data)
    .groupBy("color")
    .map((value, key) => ({ color: key, users: value }))
    .value()
);

to call chain with data and groupBy to return an array of data items grouped by the color property value.

And then we call map with a callback that sets the color to the key color value and users is set to value which has the data with the given color key value.

And then we call value to return the values.

Conclusion

To add keys for grouped output with lodash .groupBy and JavaScript, we can use the map method to add the keys returned by the groupby method.

Categories
JavaScript Answers

How to check if a string is HTML or not with JavaScript?

Sometimes, we want to check if a string is HTML or not with JavaScript.

In this article, we’ll look at how to check if a string is HTML or not with JavaScript.

How to check if a string is HTML or not with JavaScript?

To check if a string is HTML or not with JavaScript, we check if there’re any tags present in the string.

For instance, we write

const isHtml = /<\/?[a-z][\s\S]*>/i.test("<p>fizz buzz</p>");

to call /<\/?[a-z][\s\S]*>/i.test with "<p>fizz buzz</p>" to check if "<p>fizz buzz</p>" is a JavaScript string.

We use <\/?[a-z][\s\S]*> to check for any tags in the string to check if it has any HTML markup.

[a-z] checks for letters and \s\S checks for spaces and non-space characters.

Conclusion

To check if a string is HTML or not with JavaScript, we check if there’re any tags present in the string.