3

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.

1
  • 1
    most browsers support console.table to show the object in table format Commented Sep 7, 2019 at 17:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

OK, I see! Now that I've got the "pretty" JSON printing great to console using JSON.stringify(indices, null, 4) is there any way to number the objects from 1 being the first object, 2 the second etc etc? Thanks!
@tonypizza - Not via JSON.stringify, no, unless of course you include the number in the object itself as a property. You can easily do that with the map/join solution above, though; map's callback is called with three arguments, the second of which is the array index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.