Formatting text to add new lines in JavaScript and form like a table?

JavascriptWeb DevelopmentObject Oriented Programming

For this, use map() along with join(‘
’).Then ‘
’ is for new line. Following is the code −

Example

studentDetails = [
   [101, 'John', 'JavaScript'],
   [102, 'Bob', 'MySQL']
];
var studentFormat = '||Id||Name||subjName||
'; var seperate = ''; seperate = seperate + studentDetails.map(obj => `|${obj.join('|')}|`).join('
'); studentFormat = studentFormat + seperate; console.log(studentFormat);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo112.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo112.js
||Id||Name||subjName||
|101|John|JavaScript|
|102|Bob|MySQL|
raja
Updated on 09-Sep-2020 13:08:23

Advertisements