Categories
JavaScript Answers

How to write objects into file with Node.js?

Sometimes, we want to write objects into file with Node.js.

In this article, we’ll look at how to write objects into file with Node.js.

How to write objects into file with Node.js?

To write objects into file with Node.js, we can convert the object to a string with util.inspect.

For instance, we write

const util = require("util");
fs.writeFileSync("./data.json", util.inspect(obj), "utf-8");

to call fs.writeSync to write the stringified obj object into the data.json file.

We call util.inspect with obj to convert obj into a JSON string.

Conclusion

To write objects into file with Node.js, we can convert the object to a string with util.inspect.

Categories
JavaScript Answers

How to check something is empty in JavaScript?

Sometimes, we want to check something is empty in JavaScript.

In this article, we’ll look at how to check something is empty in JavaScript.

How to check something is empty in JavaScript?

To check something is empty in JavaScript, we can check the variable for null, undefined, or an empty string.

For instance, we write

if (myVar === "") {
  // ...
}

to check if myVar is an empty string.

We write

if (myVar === null) {
  // ...
}

to check if myVar is null.

And we write

if (myVar === undefined) {
  // ...
}

to check if myVar is undefined.

Conclusion

To check something is empty in JavaScript, we can check the variable for null, undefined, or an empty string.

Categories
JavaScript Answers

How to split a JavaScript array into N arrays?

Sometimes, we want to split a JavaScript array into N arrays.

In this article we’ll look at how to split a JavaScript array into N arrays.

How to split a JavaScript array into N arrays?

To split a JavaScript array into N arrays, we can use a for loop.

For instance, we write

const splitToChunks = (array, parts) => {
  const arrayCopy = [...array];
  let result = [];
  for (let i = parts; i > 0; i--) {
    result.push(arrayCopy.splice(0, Math.ceil(arrayCopy.length / i)));
  }
  return result;
};

to define the splitToChunks function that takes the array to split and the number of parts to split array into.

In it, we make a copy of the original array and assign it to arrayCopy so the original array is unaffected.

Then we use a for loop to loop from parts to 1.

And in the loop, we call arrayCopy.splice to return the slice between index 0 and Math.ceil(arrayCopy.length / i) and push returned array slice into the result array.

Conclusion

To split a JavaScript array into N arrays, we can use a for loop.

Categories
JavaScript Answers

How to filter array of objects with JavaScript?

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

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

How to filter array of objects with JavaScript?

To filter array of objects with JavaScript, we can use the array filter method.

For instance, we write

const names = [
  { name: "Joe", age: 20, email: "joe@hotmail.com" },
  { name: "Mike", age: 50, email: "mike@hotmail.com" },
  { name: "Joe", age: 45, email: "mike@hotmail.com" },
];
const foundNames = names.filter((v) => v.name === "Joe" && v.age < 30);

to call names.filter with a function at checks if the name of the object v is 'Joe' and the age property is less than 30.

If both conditions are met, then the object will be included in the returned array.

Conclusion

To filter array of objects with JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to load totally new data with Chart.js and JavaScript?

Sometimes, we want to load totally new data with Chart.js and JavaScript.

In this article, we’ll look at how to load totally new data with Chart.js and JavaScript.

How to load totally new data with Chart.js and JavaScript?

To load totally new data with Chart.js and JavaScript, we call destroy on the existing chart and then we create a new chart.

To do this, we write

myLineChart.destroy();

to destroy the myLineChart chart with destroy.

Then we write

const ctx = document.getElementById("myChartLine").getContext("2d");
myLineChart = new Chart(ctx).Line(data, options);

to get the canvas with getElementById.

Then we get the canvas context with getContext.

Finally, we create the new chart with the Chart constructor and the Line method.

Conclusion

To load totally new data with Chart.js and JavaScript, we call destroy on the existing chart and then we create a new chart.