Categories
JavaScript Answers

How to filter an array of objects that contains a string using Lodash and JavaScript?

Sometimes, we want to filter an array of objects that contains a string using Lodash and JavaScript.

In this article, we’ll look at how to filter an array of objects that contains a string using Lodash and JavaScript.

How to filter an array of objects that contains a string using Lodash and JavaScript?

To filter an array of objects that contains a string using Lodash and JavaScript, we can use the string includes method.

For instance, we write:

const collection = [{
    val: "CPP@4@1900-01-01"
  },
  {
    val: "CMIP@5@1900-01-01"
  },
  {
    val: "CMIP@6@1900-01-01"
  },
  {
    val: "CMIP@7@1900-01-01"
  },
  {
    val: "CPP@8@1900-01-01"
  },
  {
    val: "CMIP@9@1900-01-01"
  },
  {
    val: "CMIP@10@1900-01-01"
  }

];
const search = 'CPP@';
const results = _.filter(collection, (item) => {
  return item.val.includes(search);
});
console.log(results);

to call filter with the collection to filter and a callback that returns the condition of the items we want to return.

We use includes to check if item.val has search inside.

Therefore, results is:

[
  {
    "val": "CPP@4@1900-01-01"
  },
  {
    "val": "CPP@8@1900-01-01"
  }
]

Conclusion

To filter an array of objects that contains a string using Lodash and JavaScript, we can use the string includes method.

Categories
JavaScript Answers

How to make a post request with form data with JavaScript and Axios?

Sometimes, we want to make a post request with form data with JavaScript and Axios.

In this article, we’ll look at how to make a post request with form data with JavaScript and Axios.

How to make a post request with form data with JavaScript and Axios?

To make a post request with form data with JavaScript and Axios, we can call axios.post with the form data object as the 2nd argument.

For instance, we write:

const formData = new FormData()
formData.append('title', 'foo')
formData.append('description', 'bar')

axios.post('https://jsonplaceholder.typicode.com/posts', formData)

We create a new FormData instance and add some key-value pairs to it with append.

Then we call axios.post with URL to make the request to and the formData object respectively.

We can see form data sent from Chrome’s Network tab’s Payload tab of the request to https://jsonplaceholder.typicode.com/posts.

Conclusion

To make a post request with form data with JavaScript and Axios, we can call axios.post with the form data object as the 2nd argument.

Categories
JavaScript Answers

What is the async await equivalent of Promise.resolve().then() in JavaScript?

Sometimes, we want to find the async and await equivalent of Promise.resolve().then() in JavaScript.

In this article, we’ll look at how the async and await equivalent of Promise.resolve().then() in JavaScript.

What is the async await equivalent of Promise.resolve().then() in JavaScript?

The async and await equivalent of Promise.resolve().then() in JavaScript is the following:

const fn = async () => {}

(async () => {
  await fn()
})()

fn is the same as Promise.resolve which returns a promise that resolves to an empty value.

We call it and use await so that we wait until fn resolves until we run the next line of code.

Conclusion

The async and await equivalent of Promise.resolve().then() in JavaScript is an async function that returns nothing and adding await before the async function call.

Categories
JavaScript Answers

How to detect the value of input field after a keydown event in the input field with JavaScript?

Sometimes, we want to detect the value of input field after a keydown event in the input field with JavaScript.

In this article, we’ll look at how to detect the value of input field after a keydown event in the input field with JavaScript.

How to detect the value of input field after a keydown event in the input field with JavaScript?

To detect the value of input field after a keydown event in the input field with JavaScript, we can use the value property.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onkeydown = (e) => {
  console.log(e.target.value)
}

to select the input with querySelector.

And then we set the input.onkeydown property to a function that logs the input value, which is stored in e.target.value.

Conclusion

To detect the value of input field after a keydown event in the input field with JavaScript, we can use the value property.

Categories
JavaScript Answers

How to get an HTML element in which a mouse click occurred with JavaScript?

Sometimes, we want to get an HTML element in which a mouse click occurred with JavaScript.

In this article, we’ll look at how to get an HTML element in which a mouse click occurred with JavaScript.

How to get an HTML element in which a mouse click occurred with JavaScript?

To get an HTML element in which a mouse click occurred with JavaScript, we can use the e.target property in the document’s click event listener.

For instance, we write:

<div>
  foo
</div>
<section>
  bar
</section>

to add some elements.

Then we write:

document.onclick = (e) => {
  console.log(e.target.tagName)
}

to add a click event listener to document which is the page.

In the click listener, we log the e.target.tagName, which has the tag name of the element that we clicked on.

e.target is the element we clicked on.

Therefore, when we click on the div, we see 'DIV' logged.

And when we click on the section element, we see 'SECTION' logged.

Conclusion

To get an HTML element in which a mouse click occurred with JavaScript, we can use the e.target property in the document’s click event listener.