Categories
JavaScript Answers

How to set profile image as first letters of first and last name with JavaScript?

Sometimes, we want to set profile image as first letters of first and last name with JavaScript.

In this article, we’ll look at how to set profile image as first letters of first and last name with JavaScript.

How to set profile image as first letters of first and last name with JavaScript?

To set profile image as first letters of first and last name with JavaScript, we can add a div and make it round by setting the border-radius CSS property.

Then we can set the text inside the div by setting its textContent property.

For instance, we write:

<div id='profileImage'>

</div>

to add a div.

Then we write:

#profileImage {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: #512DA8;
  font-size: 35px;
  color: #fff;
  text-align: center;
  line-height: 150px;
  margin: 20px 0;
}

to add style the div.

We make it round by setting border-radius to 50%.

And we center the text by setting text-align to center and vertically center the text by setting line-height to match the height.

Next, we write:

const profileImage = document.getElementById('profileImage')
profileImage.textContent = 'JS

to select the div with getElementById and set the text by setting the textContent property.

Conclusion

To set profile image as first letters of first and last name with JavaScript, we can add a div and make it round by setting the border-radius CSS property.

Then we can set the text inside the div by setting its textContent property.

Categories
JavaScript Answers

How to fix the string replace method not replacing text containing literal \r\n strings in JavaScript?

Sometimes, we want to fix the string replace method not replacing text containing literal \r\n strings in JavaScript.

In this article, we’ll look at how to fix the string replace method not replacing text containing literal \r\n strings in JavaScript.

How to fix the string replace method not replacing text containing literal \r\n strings in JavaScript?

To fix the string replace method not replacing text containing literal \r\n strings in JavaScript, we can call replace with a regex to match all instances of \r\n and replace them with the characters we want.

For instance, we write:

const value = 'abc\r\ndef'
const newVal = value.replace(/(?:\\[rn]|[\r\n])/g, "");
console.log(newVal)

to call value.replace to replace all instances of \r\n with empty strings.

We use the /(?:\\[rn]|[\r\n])/g regex to find all instances of \r\n in value.

As a result, newVal is "abcdef".

Conclusion

To fix the string replace method not replacing text containing literal \r\n strings in JavaScript, we can call replace with a regex to match all instances of \r\n and replace them with the characters we want.

Categories
JavaScript Answers Vue Vue Answers

How to check if image URL is valid or broken with Vue.js?

Sometimes, we want to check if image URL is valid or broken with Vue.js.

In this article, we’ll look at how to check if image URL is valid or broken with Vue.js.

How to check if image URL is valid or broken with Vue.js?

To check if image URL is valid or broken with Vue.js, we can handle the error event triggered by the img element.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div>
    	<img src='abc' @error='onError'>
    </div>
  `,
  methods: {
    onError() {
      console.log('img failed to load')
    }
  }
})

to add an img element into the template.

And we set @error to onError to run onError when the image fails to load.

Since the img element’s src attribute isn’t a valid URL, onError will run.

Conclusion

To check if image URL is valid or broken with Vue.js, we can handle the error event triggered by the img element.

Categories
JavaScript Answers

How to check if all elements in array are strings with JavaScript?

Sometimes, we want to check if all elements in array are strings with JavaScript.

In this article, we’ll look at how to check if all elements in array are strings with JavaScript.

How to check if all elements in array are strings with JavaScript?

To check if all elements in array are strings with JavaScript, we can use the array every method and the typeof operator.

For instance, we write:

const check = arr => arr.every(i => (typeof i === "string"));

console.log(check(['foo', 'bar']))
console.log(check(['foo', 'bar', 1]))

to define the check function that calls arr.every with a callback that checks of each entry i is a string with typeof.

As a result, the first console log logs true and the 2nd one logs false since the first array have all string entries but the 2nd array has one entry that’s not a string.

Conclusion

To check if all elements in array are strings with JavaScript, we can use the array every method and the typeof operator.

Categories
JavaScript Answers

How to map multiple arrays with JavaScript?

Sometimes, we want to map multiple arrays with JavaScript.

In this article, we’ll look at how to map multiple arrays with JavaScript.

How to map multiple arrays with JavaScript?

To map multiple arrays with JavaScript, we can use the map method.

For instance, we write:

const zip = (a1, a2) => a1.map((x, i) => [x, a2[i]]);

const arr1 = ['a', 'b', 'c'];
const arr2 = [1, 2, 3];
console.log(zip(arr1, arr2))

to define the zip function that calls a1.map to combine the entries from a1 and a2 with index i into one entry.

Then we call zip with arr1 and arr2 to zip them into one array.

As a result, the array logged is [["a", 1], ["b", 2], ["c", 3]].

Conclusion

To map multiple arrays with JavaScript, we can use the map method.