tonypizza
tonypizza

Reputation: 86

How to insert newline into an array of created Javascript objects

I create an empty array, create object, drop existing key:value data into the object, push object into array, sort the array's objects by one key:value pair, stringify the array, and console.log it.

This all works fine. But if I try to insert a newline using \n the data inside each object is no longer returned when I console.log the stringified array. Instead, I just get [object, Object] on the console.

I've tried to put \n everywhere. Inside the created object as "\n" and also as \n. After the created object as "\n" or \n. Before I stringify, after I stringify. Nothing seems to work.

//create new empty array called indices
var indices = [];
//create new object called newobject and fill it with key:value pair data
var newobject = { name: y[i].Name, age: y[i].age };
//push newobject into indices array
indices.push(newobject);
//create new object called newobject2 and fill it with key:value pair data
varnewobject2 = { country: y[i].Name, age: y[i].age, height: y[i].height };
//push newobject2 into indices array
indices.push(newobject2);
//sort objects in indices array by age values lowest to highest
indices.sort((a, b) => a.age - b.age);
//new variable called output is the sorted indices array stringified
var output = JSON.stringify(indices);
//console.log variable output
console.log(output);

I want each created object in the array to print to a new line when I console.log the stringified array all these new objects are sitting in. The above code works. I just have no idea where or how to insert a line break after each new object. Whenever I try to insert \n the output given reads as:

[output Output]

Thanks for your kind consideration.

Upvotes: 3

Views: 5524

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074295

You could just let JSON.stringify to do formatting for you:

var output = JSON.stringify(indices, null, 0);

...but it won't put a newline between the opening [ and the first object:

var indices = [];
var newobject = { name: "name1", age: 21 };
indices.push(newobject);
var newobject2 = { country: "country1", age: 42, height: 6 };
indices.push(newobject2);
indices.sort((a, b) => a.age- b.age);
var output = JSON.stringify(indices, null, 0);
console.log(output);

You can use a value greater than 4 and get well-formatted output, but not quite as you asked for it:

var indices = [];
var newobject = { name: "name1", age: 21 };
indices.push(newobject);
var newobject2 = { country: "country1", age: 42, height: 6 };
indices.push(newobject2);
indices.sort((a, b) => a.age- b.age);
var output = JSON.stringify(indices, null, 4);
console.log(output);

Alternately, since you know the outermost bit is an array, you could just loop through and create the string yourself:

var output = "[\n" +
  indices.map(entry => JSON.stringify(entry)).join(",\n") +
  "\n]";

var indices = [];
var newobject = { name: "name1", age: 21 };
indices.push(newobject);
var newobject2 = { country: "country1", age: 42, height: 6 };
indices.push(newobject2);
indices.sort((a, b) => a.age- b.age);
var output = "[\n" +
  indices.map(entry => JSON.stringify(entry)).join(",\n") +
  "\n]";
console.log(output);

Upvotes: 8

Related Questions