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.

Categories
JavaScript Answers

How to remove undefined values from array with JavaScript?

Sometimes, we want to remove undefined values from array with JavaScript.

In this article, we’ll look at how to remove undefined values from array with JavaScript.

How to remove undefined values from array with JavaScript?

To remove undefined values from array with JavaScript, we call the filter method.

For instance, we write

const data = [42, 21, undefined, 50, 40, undefined, 9];

const newData = data.filter((element) => {
  return element !== undefined;
});

to call data.filter with a callback that checks if each element in data isn’t undefined.

Then items that aren’t undefined are returned in a new array.

Conclusion

To remove undefined values from array with JavaScript, we call the filter method.