Categories
JavaScript Answers

How to automatically add properties to an object that is undefined with JavaScript?

Sometimes, we want to automatically add properties to an object that is undefined with JavaScript.

In this article, we’ll look at how to automatically add properties to an object that is undefined with JavaScript.

How to automatically add properties to an object that is undefined with JavaScript?

To automatically add properties to an object that is undefined with JavaScript, we can use the nullish assignment operator.

For instance, we write

const test = {};
test.hello ??= {};
test.hello.world ??= "Hello";

to create the empty test object.

Then we assign test.hello to an empty object if it doesn’t exist with

test.hello ??= {};

Then if test.hello.world doesn’t exist, we assign it to "Hello" with

test.hello.world ??= "Hello";

Conclusion

To automatically add properties to an object that is undefined with JavaScript, we can use the nullish assignment operator.

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.