Categories
JavaScript Answers

How to create object from array with JavaScript?

Sometimes, we want to create object from array with JavaScript.

In this article, we’ll look at how to create object from array with JavaScript.

How to create object from array with JavaScript?

To create object from array with JavaScript, we call the Object.fromEntries method.

For instance, we write

const dynamicArray = ["2007", "2008", "2009", "2010"];
const obj = Object.fromEntries(
  dynamicArray.map((year) => [
    year,
    {
      something: "based",
      on: year,
    },
  ])
);

console.log(obj);

to call dynamicArray.map with a callback that maps each entry to an array that has the key and value respectively.

Then we call Object.fromEntries with the array to return an object with the year values as the keys and the objects as the values.

Conclusion

To create object from array with JavaScript, we call the Object.fromEntries method.

Categories
JavaScript Answers

How to delete an item using Redux and JavaScript?

Sometimes, we want to delete an item using Redux and JavaScript.

In this article, we’ll look at how to delete an item using Redux and JavaScript.

How to delete an item using Redux and JavaScript?

To delete an item using Redux and JavaScript, we return a new array without the removed item.

For instance, we write

function cartReducer(state = initialState, action) {
  const { id } = action.payload;
  switch (action.type) {
    //...
    case actionTypes.DELETE_CART:
      return {
        ...state,
        carts: state.carts.filter((item) => item.id !== payload),
      };
  }
}

to add a switch statement into the cartReducer function.

In it, we check if the DELETE_CART action is invoked.

If it is, then we return an object with the new state that has the removed item gone.

We set carts to a new array that has the item that does have id equal to payload.

Conclusion

To delete an item using Redux and JavaScript, we return a new array without the removed item.

Categories
JavaScript Answers

How to get current date with Moment and JavaScript?

Sometimes, we want to get current date with Moment and JavaScript.

In this article, we’ll look at how to get current date with Moment and JavaScript.

How to get current date with Moment and JavaScript?

To get current date with Moment and JavaScript, we use the moment function without any arguments.

For instance, we write

const datetime = moment();

to call moment to get a moment object with the current date.

Conclusion

To get current date with Moment and JavaScript, we use the moment function without any arguments.

Categories
JavaScript Answers

How to expect arrays to be equal ignoring order with Jest and JavaScript?

Sometimes, we want to expect arrays to be equal ignoring order with Jest and JavaScript.

In this article, we’ll look at how to expect arrays to be equal ignoring order with Jest and JavaScript.

How to expect arrays to be equal ignoring order with Jest and JavaScript?

To expect arrays to be equal ignoring order with Jest and JavaScript, we can use the toEqual method to compare an array of primitive values.

For instance, we write

expect(array1.sort()).toEqual(array2.sort());

to call sort to sort array1 and array2.

Then we call toEqual with the sorted version of array2 to see if it’s the same as the sorted version of array1.

This works if array1 and array2 only have primitive values.

If they don’t have primitive values, then we’ve to convert them to arrays that have primitive values.

For instance, we write

const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
const array2 = [{ id: 3 }, { id: 2 }, { id: 1 }];

expect(array1.map((a) => a.id).sort()).toEqual(array2.map((a) => a.id).sort());

to call map to map both arrays to arrays with only the id property values.

And then we do the comparisons.

Conclusion

To expect arrays to be equal ignoring order with Jest and JavaScript, we can use the toEqual method to compare an array of primitive values.

Categories
JavaScript Answers

How to use regex to replace everything except numbers and a decimal point with JavaScript?

Sometimes, we want to use regex to replace everything except numbers and a decimal point with JavaScript.

In this article, we’ll look at how to use regex to replace everything except numbers and a decimal point with JavaScript.

How to use regex to replace everything except numbers and a decimal point with JavaScript?

To use regex to replace everything except numbers and a decimal point with JavaScript, we can use the string replace method.

For instance, we write

const n = number.replace(/(\.\d+)+/, "");

to replace all digits after the decimal point with empty strings.

Conclusion

To use regex to replace everything except numbers and a decimal point with JavaScript, we can use the string replace method.