How to insert newline into an array of created Javascript objects - Stack Overflow

admin2025-04-09  0

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.

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.

Share Improve this question edited Sep 7, 2019 at 17:37 tonypizza asked Sep 7, 2019 at 17:21 tonypizzatonypizza 861 silver badge5 bronze badges 1
  • 1 most browsers support console.table to show the object in table format – Slai Commented Sep 7, 2019 at 17:47
Add a ment  | 

1 Answer 1

Reset to default 8

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);

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744128445a232570.html

最新回复(0)