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.

Categories
JavaScript Answers

How to call JavaScript function from URL or address bar?

Sometimes, we want to call JavaScript function from URL or address bar.

In this article, we’ll look at how to call JavaScript function from URL or address bar.

How to call JavaScript function from URL or address bar?

To call JavaScript function from URL or address bar, we can type in a data URL.

For instance, we type in

data:text/html,<script>alert('hi');</script>

into the address bar to run the JavaScript code in the script tag.

Therefore, we see an alert box that shows ‘hi’ displayed when we type this in.

Conclusion

To call JavaScript function from URL or address bar, we can type in a data URL.

Categories
React Answers

How to change child component’s state from parent component with React?

Sometimes, we want to change child component’s state from parent component with React.

In this article, we’ll look at how to change child component’s state from parent component with React.

How to change child component’s state from parent component with React?

To change child component’s state from parent component with React, we can pass props.

For instance, we write

const Child = ({ open }) => {
  return <Drawer open={open} />;
};

const ParentComponent = () => {
  const [isOpen, setIsOpen] = useState(false);

  const toggleChildMenu = () => {
    setIsOpen((prevValue) => !prevValue);
  };

  return (
    <div>
      <button onClick={toggleChildMenu}>Toggle Menu from Parent</button>
      <Child open={isOpen} />
    </div>
  );
};

to set the open prop of the Child component to the isOpen state’s value in ParentComponent.

Then in child we pass the open prop as the value of the open prop of the Drawer.

Conclusion

To change child component’s state from parent component with React, we can pass props.

Categories
JavaScript Answers

How to replace a regex substring match in JavaScript?

Sometimes, we want to replace a regex substring match in JavaScript.

In this article, we’ll look at how to replace a regex substring match in JavaScript.

How to replace a regex substring match in JavaScript?

To replace a regex substring match in JavaScript, we can use the string replace method with a regex.

For instance, we write

let str = "asd-0.testing";
const regex = /\d/;
str = str.replace(regex, "1");
console.log(str);

to call str.replace with regex and 1 to replace the digits in str with 1.

We use \d to match a single digit in the string.

Conclusion

To replace a regex substring match in JavaScript, we can use the string replace method with a regex.