Categories
JavaScript Answers

How to extract time from moment.js object with JavaScript?

Sometimes, we want to extract time from moment.js object with JavaScript.

In this article, we’ll look at how to extract time from moment.js object with JavaScript.

How to extract time from moment.js object with JavaScript?

To extract time from moment.js object with JavaScript, we can use the format method.

For instance, we write

const time = moment("2022-01-16T12:00:00").format("hh:mm:ss a");

to call format with "hh:mm:ss a" to return the time string of the date time in hours:minutes:seconds AM/PM format.

Conclusion

To extract time from moment.js object with JavaScript, we can use the format method.

Categories
JavaScript Answers

How to merge objects by id with JavaScript?

Sometimes, we want to merge objects by id with JavaScript.

In this article, we’ll look at how to merge objects by id with JavaScript.

How to merge objects by id with JavaScript?

To merge objects by id with JavaScript, we use the map method and the spread operator.

For instance, we write

const a1 = [
  { id: 1, name: "test" },
  { id: 2, name: "test2" },
];
const a2 = [
  { id: 1, count: "1" },
  { id: 2, count: "2" },
];
const a3 = a1.map((t1) => ({ ...t1, ...a2.find((t2) => t2.id === t1.id) }));

to call a1.map with a callback that returns a new object with the properties of the object being looped through t1 spread into a new object.

And we get the object with the same id value as t1 with a2.find((t2) => t2.id === t1.id) and spread its properties into the same object.

The properties of the object that’s merged in the latest would override the property values of the items merged in earlier if they have the same property name.

Conclusion

To merge objects by id with JavaScript, we use the map method and the spread operator.

Categories
JavaScript Answers

How to get the length of a JavaScript associative array?

Sometimes, we want to get the length of a JavaScript associative array.

In this article, we’ll look at how to get the length of a JavaScript associative array.

How to get the length of a JavaScript associative array?

To get the length of a JavaScript associative array, we use the Object.keys method.

For instance, we write

const myObject = {};
myObject["q101"] = "Your name?";
myObject["q102"] = "Your age?";
myObject["q103"] = "Your school?";
console.log(Object.keys(myObject).length);

to define the myObject object.

Then we call Object.keys with myObject to return an array of non-inherited property key strings.

And then we get the length property of that to get the number of non-inherited properties in the object.

Conclusion

To get the length of a JavaScript associative array, we use the Object.keys method.

Categories
JavaScript Answers

How to convert milliseconds into date and time with Moment.js and JavaScript?

Sometimes, we want to convert milliseconds into date and time with Moment.js and JavaScript.

In this article, we’ll look at how to convert milliseconds into date and time with Moment.js and JavaScript.

How to convert milliseconds into date and time with Moment.js and JavaScript?

To convert milliseconds into date and time with Moment.js and JavaScript, we use the format method.

For instance, we write

const dateTime = moment(1454521239279).format("DD MMM YYYY hh:mm a");

to call moment with the millisecond timestamp.

Then we call format to convert the timestamp into a string with the human readable date and time.

Conclusion

To convert milliseconds into date and time with Moment.js and JavaScript, we use the format method.

Categories
JavaScript Answers

How to print part of a rendered HTML page in JavaScript?

Sometimes, we want to print part of a rendered HTML page in JavaScript.

In this article, we’ll look at how to print part of a rendered HTML page in JavaScript.

How to print part of a rendered HTML page in JavaScript?

To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.

For instance, we write

const newWindow = window.open();
newWindow.document.write(document.getElementById("output").innerHTML);
newWindow.print();

to call window.open to open a new window.

Then we call newWindow.document.write to render the HTML string of the element we want to print.

We get the element with getElementById and get the string with the HTML code for the element with innerHTML.

Next we call print to open the print dialog to print the stuff in the new window.

Conclusion

To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.