Categories
JavaScript Answers

How to add an image placeholder with JavaScript?

Sometimes, we want to add an image placeholder with JavaScript.

In this article, we’ll look at how to add an image placeholder with JavaScript.

How to add an image placeholder with JavaScript?

To add an image placeholder with JavaScript, we can handle the img element’s error event.

For instance, we write:

<img src='abc'>

to add an img element.

Then we write:

const img = document.querySelector('img')
img.onerror = (e) => {
  e.target.src = 'https://picsum.photos/200/300'
}

to select the img element with querySelector.

Next, we set the img.onerror property to a function that sets the img’s src property to the placeholder image’s URL.

Therefore, when the image fails to load, onerror will run and set the src attribute to the placeholder image URL.

Conclusion

To add an image placeholder with JavaScript, we can handle the img element’s error event.

Categories
JavaScript Answers

How to get the key and value of an object in JavaScript?

Sometimes, we want to get the key and value of an object in JavaScript.

In this article, we’ll look at how to get the key and value of an object in JavaScript.

How to get the key and value of an object in JavaScript?

To get the key and value of an object in JavaScript, we can use the Object.entries method.

For instance, we write:

const topBrands = [{
  'Adidas': 100
}, {
  'Nike': 50
}];

for (const t of topBrands) {
  for (const [key, val] of Object.entries(t)) {
    console.log(key, val)
  }
}

to loop through the entries of the topBrands array with the for-of loop.

In the loop, we get the key-value entries as an array with Object.entries.

Then we loop through the key-value arrays returned with another for-of loop and log the key and val values.

Therefore, we see:

"Adidas", 100
"Nike", 50

logged.

Conclusion

To get the key and value of an object in JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to check if array has elements from another array with JavaScript?

Sometimes, we want to check if array has elements from another array with JavaScript.

In this article, we’ll look at how to check if array has elements from another array with JavaScript.

How to check if array has elements from another array with JavaScript?

To check if array has elements from another array with JavaScript, we can use the array some and includes method.

For instance, we write:

const a = [1, 2, 3]
const b = [1, 4, 5]
const includesB = a.some(e => b.includes(e))
console.log(includesB)

to call a.some with a callback that returns b.includes(e) to check any element in a is also in b.

Since 1 is in a and b, includesB is true.

Conclusion

To check if array has elements from another array with JavaScript, we can use the array some and includes method.

Categories
JavaScript Answers

How to find if object property exists in array with JavaScript and Lodash?

Sometimes, we want to find if object property exists in array with JavaScript and Lodash.

In this article, we’ll look at how to find if object property exists in array with JavaScript and Lodash.

How to find if object property exists in array with JavaScript and Lodash?

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

For instance, we write:

const data = [{
    "name": "apple",
    "id": "apple0"
  },
  {
    "name": "dog",
    "id": "dog1"
  },
  {
    "name": "cat",
    "id": "cat2"
  }
]

const apple = _.find(data, {
  name: 'apple'
})
console.log(apple)

to call _.find with data with the property key and value we’re looking for.

Therefore, apple is {name: 'apple', id: 'apple0'}.

Conclusion

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

Categories
JavaScript Answers

How to convert a number to a letter with JavaScript?

Sometimes, we want to convert a number to a letter with JavaScript.

In this article, we’ll look at how to convert a number to a letter with JavaScript.

How to convert a number to a letter with JavaScript?

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.

For instance, we write:

const i = 5
const c = String.fromCharCode(94 + i);
console.log(c)

We call String.fromCharCode with 94 + i to convert i to a letter.

Therefore, c is 'c' since 97 is the code for 'c'‘ in ASCII.

Conclusion

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.