Categories
JavaScript Answers

How to filter an array with a function that returns a promise with JavaScript?

Sometimes, we want to filter an array with a function that returns a promise with JavaScript.

In this article, we’ll look at how to filter an array with a function that returns a promise with JavaScript.

How to filter an array with a function that returns a promise with JavaScript?

To filter an array with a function that returns a promise with JavaScript, we call filter with an async function.

For instance, we write

const results = await filter(myArray, async (num) => {
  await doAsyncStuff();
  return num > 2;
});

to call filter with myArray and an async function which always returns a promise.

An array of promises after filtering is done is returned.

Then we use await to get the array of promise results and assign the results to results.

Conclusion

To filter an array with a function that returns a promise with JavaScript, we call filter with an async function.

Categories
JavaScript Answers

How to map array last item with JavaScript?

Sometimes, we want to map array last item with JavaScript.

In this article, we’ll look at how to map array last item with JavaScript.

How to map array last item with JavaScript?

To map array last item with JavaScript, we check the items’ index in the map callback.

For instance, we write

const newRows = row.map((rank, i, row) => {
  if (i + 1 === row.length) {
    // Last one.
  } else {
    // Not last one.
  }
});

to call row.map to check if index i + 1 is row.length.

If it is, then the callback is called on the last item in row.

Otherwise, it’s not called on the last object.

Conclusion

To map array last item with JavaScript, we check the items’ index in the map callback.

Categories
JavaScript Answers

How to correctly use axios params with JavaScript arrays?

Sometimes, we want to correctly use axios params with JavaScript arrays.

In this article, we’ll look at how to correctly use axios params with JavaScript arrays.

How to correctly use axios params with JavaScript arrays?

To correctly use axios params with JavaScript arrays, we serialize them to a string.

For instance, we write

axios.get("/myController/myAction", {
  params: {
    storeIds: [1, 2, 3],
  },
  paramsSerializer: (params) => {
    return qs.stringify(params);
  },
});

to call get with the storeIds params.

We set the paramsSerializer property to a function that returns the stringified parameters we get from qs.stringify.

stringify converts the params object into a query string with all the parameters encoded.

Conclusion

To correctly use axios params with JavaScript arrays, we serialize them to a string.

Categories
JavaScript Answers

How to declare array of objects with JavaScript?

Sometimes, we want to declare array of objects with JavaScript.

In this article, we’ll look at how to declare array of objects with JavaScript.

How to declare array of objects with JavaScript?

To declare array of objects with JavaScript, we declare an array with object literals.

For instance, we write

const sample = [{}, {}, {} /*, ... */];

to declare the sample array with empty objects inside it.

Conclusion

To declare array of objects with JavaScript, we declare an array with object literals.

Categories
JavaScript Answers

How to remove empty strings from array while keeping record without loops with JavaScript?

Sometimes, we want to remove empty strings from array while keeping record without loops with JavaScript.

In this article, we’ll look at how to remove empty strings from array while keeping record without loops with JavaScript.

How to remove empty strings from array while keeping record without loops with JavaScript?

To remove empty strings from array while keeping record without loops with JavaScript, we call the filter method.

For instance, we write

const arr = ["I", "am", "", "still", "here", "", "man"];
const newArr = arr.filter(Boolean);

to call arr.filter with Boolean to return a new array without the falsy values in arr.

Empty string is a falsy value so it’s filtered out.

Conclusion

To remove empty strings from array while keeping record without loops with JavaScript, we call the filter method.