Categories
JavaScript Answers

How to reverse an array in JavaScript without using libraries?

Sometimes, we want to reverse an array in JavaScript without using libraries.

In this article, we’ll look at how to reverse an array in JavaScript without using libraries.

How to reverse an array in JavaScript without using libraries?

To reverse an array in JavaScript without using libraries, we use the reverse method.

For instance, we write

const original = [1, 2, 3, 4];
const reversed = [...original].reverse();

to make a copy of the original array with the spread operator.

Then we reverse the copied array with reverse.

Conclusion

To reverse an array in JavaScript without using libraries, we use the reverse method.

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.