Categories
JavaScript Answers

How to convert JSON to CSV format and store in a variable with JavaScript?

Sometimes, we want to convert JSON to CSV format and store in a variable with JavaScript.

In this article, we’ll look at how to convert JSON to CSV format and store in a variable with JavaScript.

How to convert JSON to CSV format and store in a variable with JavaScript?

To convert JSON to CSV format and store in a variable with JavaScript, we can use some array methods.

For instance, we write

const replacer = (key, value) => (value === null ? "" : value);
const header = Object.keys(items[0]);
const csv = [
  header.join(","),
  ...items.map((row) =>
    header
      .map((fieldName) => JSON.stringify(row[fieldName], replacer))
      .join(",")
  ),
].join("\r\n");

console.log(csv);

to get the header arrat with Object.keys called on the first entry in the items array.

Then we create the csv array by creating the header row with header.join(",").

Then we call items.map to map the rest of the entries in items to csv row strings.

And then we join the row strings with '\r\n' with join.

Conclusion

To convert JSON to CSV format and store in a variable with JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to run JavaScript when an element loses focus?

Sometimes, we want to run JavaScript when an element loses focus.

In this article, we’ll look at how to run JavaScript when an element loses focus.

How to run JavaScript when an element loses focus?

To run JavaScript when an element loses focus, we can listen for the blur event.

For instance, we write

<input type="text" name="name" value="value" />

to add an input.

Then we write

const input = document.querySelector("input");
input.onblur = () => {
  alert(1);
};

to select the input with querySelector.

And then we set input.onblur to a function that calls alert to show an alert box when we move our focus away from the input.

Conclusion

To run JavaScript when an element loses focus, we can listen for the blur event.

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.