Ways to showcase every input within an array of objects using Javascript

Assume I have created an array of objects as shown below:

 var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];

When running

console.log(person); 

the output is "[Object,Object]"

I am looking to display the details of the array. What steps should I take?

Answer №1

If you want a cleaner output, give this a shot: console.table(user);

The tabular format makes it easier to read and understand the object's data. Here's how the result would appear: https://example.com/tabular_output.png

While not as popular as console.log, console.table can be quite useful in certain situations.

Answer №2

console.log(JSON.stringify(user));

This is just one method to accomplish that task.

Answer №3

let people = [{name:"Sarah", age:30, profession:"engineer"}, {name:"Michael", age:45, profession:"teacher"}] ;

console.log(JSON.stringify(people));

// loop through objects in array
for (let i = 0; i < people.length; i++) {
    let person = people[i];
    for (let key in person) {
        console.log('Person ' + i + ': ' + key + '=' + person[key]);
    }
}

Does this meet your requirements? If not, please provide more details.

Answer №4

Like many have pointed out before, utilizing JSON.stringify can help with formatting. However, your JSON data already includes collapsible Objects with all the necessary information. For a more polished output, consider using tools like beautify-js or lodash.js.

Answer №5

Here's a method you can use:

var people = [{name:"Sam", surname:"Smith", age:25, eyes:"brown"}, 
{name:"Lily", surname:"Jones", age:30, eyes:"green"}];

JSON.stringify(people);

Answer №6

Give this a shot:

const data = JSON.stringify([
    {name: "Alice", age: 30, job: "developer"},
    {name: "Bob", age: 25, job: "designer"}
  ], null, " ");

document.querySelector("#output").textContent = data;

// or you can check the result in the console
console.log(data);
<pre id="output"></pre>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Searching through a json object using typescript

While attempting to retrieve JSON data from a URL, I encountered a particular issue. The problem lies in the fact that each row in the Datarows array contains 5 rows, each row consisting of 47 cells. Out of these 47 cells in each row, I am specifically in ...

Plow through the contents of the S3 Bucket, exploring every Folder and File

I've encountered an issue while iterating through an S3 bucket using s3.listObjects. The error message { [UnexpectedParameter: Unexpected key 'Key' found in params] has been popping up. Below is the snippet of code I'm currently working ...

AngularJS seems to be ignoring CSS styles when displaying list items in the ng-repeat loop

After coming across a tutorial on creating round circles connected by a line in CSS, I decided to implement it myself. Interestingly, everything worked perfectly fine when I tested it with static HTML. However, when I tried to make it dynamic using Angular ...

Choose an image to be displayed at either full width or full height, depending on which dimension is reached first

I have a query regarding resizing an image within a div to either 100% width or 100% height of the containing div. Despite setting max-height and max-width to 100% as recommended here, I still encounter overflow issues without wanting to crop the image usi ...

AJAX does not execute all inline JavaScript code

I am currently using AJAX to load a fragment of a document. I have successfully loaded 'external' scripts, but I am encountering issues when trying to execute all the JavaScript within <script> tags. Below is an example of the HTML fragmen ...

Python transforming arguments to a list

Here's what I need: def __init__(self, *args): list_of_args = #wizardry Parent.__init__(self, list_of_args) I want to consolidate *args into a single array, so that: MyClass.__init__(a, b, c) == Parent.__init__([a, b, c]) ...

Is there a way to retrieve the href value from a modal window upon clicking the link?

When clicking on the a href, I am attempting to retrieve a value in my pop-up window. I am using magnificPopup JavaScript Below is the code for my a href tag and I want to fetch the "data-stream" value in the pop-up window. <a class="popup-with-zoom ...

Error: The code is unable to access the '0' property of an undefined variable, but it is functioning properly

I am working with two arrays in my code: bookingHistory: Booking[] = []; currentBookings: any[] = []; Both arrays are populated later in the code. The bookingHistory array consists of instances of Booking, while currentBookings contains arrays of Booking ...

Struggling with jQuery and the "hash" functionality?

I am encountering issues with jQTouch. The problem arises when I try to use this link: <a href="#site_map" class="swap">Map</a> and initialize jQTouch like this: var jQT = new $.jQTouch({ icon: 'jqtouch.png', ...

Dynamic Backbone.js Models

In the process of developing a Web Application using Backbone.js. I have a web service providing information on the required fields for my model. Therefore, creating a static model is not possible. How can I create a dynamic model for my application that ...

Exploring the differences between two-dimensional arrays using jQuery

I am currently working on comparing two 2D arrays and I only want them to match when they are completely identical. The code I have is too lengthy, especially considering the arrays could potentially be much longer. I have experimented with .each() and f ...

React: Using Chart.js to visualize negative values with a different color in the area

I am implementing a line chart using Chart.js and react-chartjs-2 to display positive and negative values. For positive values, I want the filled area to be green, and for negative values, I want it to be red. Check out the code here import React from &qu ...

Using Rails to render a partial through Ajax and then modifying that partial using JavaScript

My application allows users to click a button that triggers the appearance of a text field using ajax. After the text field appears, I am attempting to focus on it. Interestingly, $("#my-textfield").focus() works perfectly when executed in the console, b ...

Having difficulties executing a JavaScript file in the command prompt

I'm having trouble running a JavaScript file in the command prompt. Can anyone assist me with this issue? D:\>Node Welcome to Node.js v12.14.1. Type ".help" for more information. > 001.js undefined > Node 001.js Thrown: Node 001.js ...

Using $.bind() on SVG elements causes an error when a new SVG replaces the original one

I have a customized svg where elements are connected to user clicks and keyups. When a user modifies a text field on the website, it automatically updates the corresponding text element within the svg. Similarly, if the user edits the svg, the correspondin ...

Class-validator does not have any associated metadata

Struggling with implementing a ValidationPipe in my code, I consistently encounter the warning "No metadata found. There is more than one class-validator version installed probably. You need to flatten your dependencies" when sending a request. The struct ...

Using Node.js to Perform Testing with VCR

Does anyone know of a testing system similar to VCR for node.js? Check out this link for more information. ...

Seeking assistance in optimizing my Javascript code for more efficient canvas rendering

I've created a script for generating random moving lines as a background element for my portfolio. While it runs smoothly on its own, I encounter frame drops when combining it with other CSS animations and effects, especially at the beginning (althoug ...

Use two separate AJAX requests to open and close the modal dialog

I am experiencing an issue with my subscription form that uses modal windows. After closing the window and trying to subscribe again, the ajax calls are being duplicated, resulting in an error. $("#openModal").click(function() { if($("#wname").val() = ...

How come my Material UI typography isn't adjusting to different screen sizes even with the assigned theme

I am experiencing difficulties in getting my text to shrink with the viewport using material UI. Despite following the examples in the material UI docs, I can only get it to wrap, not shrink as desired. import React, { useState } from 'react'; im ...