Categories
JavaScript Answers

How to use .join method to convert array to string without commas with JavaScript?

Sometimes, we want to use .join method to convert array to string without commas with JavaScript.

In this article, we’ll look at how to use .join method to convert array to string without commas with JavaScript.

How to use .join method to convert array to string without commas with JavaScript?

To use .join method to convert array to string without commas with JavaScript, we call it with an empty string.

For instance, we write

const s = arr.join("")

to call join on the arr array with an empty string to join the strings in arr without commas.

Conclusion

To use .join method to convert array to string without commas with JavaScript, we call it with an empty string.

Categories
CSS

How to make Twitter Bootstrap tooltips have multiple lines with CSS?

Sometimes, we want to make Twitter Bootstrap tooltips have multiple lines with CSS.

In this article, we’ll look at how to make Twitter Bootstrap tooltips have multiple lines with CSS.

How to make Twitter Bootstrap tooltips have multiple lines with CSS?

To make Twitter Bootstrap tooltips have multiple lines with CSS, we can set the white-space style to pre-wrap.

For instance, we write

.tooltip-inner {
  white-space: pre-wrap;
}

to style the tooltip with white-space set to pre-wrap.

Conclusion

To make Twitter Bootstrap tooltips have multiple lines with CSS, we can set the white-space style to pre-wrap.

Categories
JavaScript Answers

How to convert XML to JSON and back using JavaScript?

Sometimes, we want to convert XML to JSON and back using JavaScript.

In this article, we’ll look at how to convert XML to JSON and back using JavaScript.

How to convert XML to JSON and back using JavaScript?

To convert XML to JSON and back using JavaScript, we can use the txml package.

To install it, we run

npm i txml

Then we use it by writing

const xml = require("txml");
const data = `
<tag>tag content</tag>
<tag2>another content</tag2>
<bar>
  <foo>inside content</foo>
  <emptyTag />
</bar>`;

const dom = xml(data);
xml.stringify(dom);

const json = xml.parse(data);

to call xml with the data XML string to convert it to a DOM object.

And we call xml.stringify to convert the dom object back to an XML string.

We call xml.parse with data to convert the XML string to a JavaScript object.

Conclusion

To convert XML to JSON and back using JavaScript, we can use the txml package.

Categories
JavaScript Answers

How to fix Uncaught TypeError: this.props.data.map is not a function with React?

Sometimes, we want to fix Uncaught TypeError: this.props.data.map is not a function with React.

In this article, we’ll look at how to fix Uncaught TypeError: this.props.data.map is not a function with React.

How to fix Uncaught TypeError: this.props.data.map is not a function with React?

To fix Uncaught TypeError: this.props.data.map is not a function with React, we can check if this.props.data is an array.

For instance, we write

{
  Array.isArray(this.props.data) &&
    this.props.data.map((e) => {
      //...
    });
}

to call Array.isArray to check if this.props.data is an array.

If it is, then we call this.props.data.map to render the items from the data entries.

Conclusion

To fix Uncaught TypeError: this.props.data.map is not a function with React, we can check if this.props.data is an array.

Categories
JavaScript Answers

How to match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?

Sometimes, we want to match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all().

In this article, we’ll look at how to match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all().

How to match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?

To match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all(), we can use the string matchAll method.

For instance, we write

const regexp = /(?:&|&amp;)?([^=]+)=([^&]+)/g;
const str = "1111342=Adam%20Franco&348572=Bob%20Jones";

for (const match of str.matchAll(regexp)) {
  const [full, key, value] = match;
  console.log(key, value);
}

to call str.matchAll to match the regex for a query string.

We use a for of loop to get all the matches.

In the loop, we get the full string match and the key and value of the query string from the match.

Conclusion

To match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all(), we can use the string matchAll method.