How to print the content of an object in JavaScript ?
Last Updated :
07 Mar, 2024
To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data.
Let's first create a JavaScript Object containing some key-values as given below:
JavaScript
// Given Object
const obj = {
name: 'John',
age: 30,
city: 'New York'
};
Examples to print the content of an object in JavaScript
1. Using JSON.stringify to print object content
JSON.stringify()
is a method in JavaScript used to convert JavaScript objects into JSON strings. We can use JSON.stringify to print the content of an object in JavaScript.
Syntax:
JSON.stringify(value, replacer, space);
Example: This example converts the object to a string by simply traversing it and appending the object property and value to the string.Â
JavaScript
let GFG_object = {
prop_1: 'val_11',
prop_2: 'val_12',
prop_3: 'val_13'
};
let printObj = function (obj) {
let string = '';
for (let prop in obj) {
if (typeof obj[prop] == 'string') {
string += prop + ': ' + obj[prop] + '; \n';
}
else {
string += prop + ': { \n' + print(obj[prop]) + '}';
}
}
return string;
}
console.log(printObj(GFG_object));
Outputprop_1: val_11;
prop_2: val_12;
prop_3: val_13;
Explanation: This code defines an object `GFG_object` with properties and values. It also defines a function `printObj` to print the contents of an object. It recursively iterates over the object's properties, printing each property and its value. The function handles nested objects by recursively calling itself.
2. Using console.log
to print object content
To print the content of an object in JavaScript, we could use console.log
Example: Here, we are using console.log statement to print the content of an object in JavaScript.
JavaScript
const myObject = { key1: 'value1', key2: 'value2', key3: 'value3' };
console.log(myObject);
Output{ key1: 'value1', key2: 'value2', key3: 'value3' }
Explanation: Here, we have use console to print the content of object.
3. Using Object.values()
The Object.values()
method in JavaScript returns an array of a given object's own enumerable property values. It extracts the property values and stores them in an array.
Example: This example prints all the values in the given object.
JavaScript
const obj = {
name: 'John',
age: 30,
city: 'New York'
};
const values = Object.values(obj);
console.log(values); // Output: ['John', 30, 'New York']
Output[ 'John', 30, 'New York' ]
Explanation: The code defines an object named `obj` with properties like `name`, `age`, and `city`. Then, it uses `Object.values(obj)` to extract the property values of `obj` and store them in an array named `values`. Finally, it prints the array `values`, which contains the values of the properties in the `obj` object.
4. Using for in loop
To print the content of an object in JavaScript, we could Loop through Object Properties.
Example: Here, we are Looping through Object Properties to print the content of an object in JavaScript.
JavaScript
const myObject = { key1: 'value1', key2: 'value2', key3: 'value3' };
for (const key in myObject) {
if (myObject.hasOwnProperty(key)) {
console.log(`${key}: ${myObject[key]}`);
}
}
Outputkey1: value1
key2: value2
key3: value3
Explanation: The code iterates through each key of the `myObject`. For each key, it checks if it's a direct property of `myObject`, then prints the key-value pair using template literals.
Similar Reads
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
3 min read
How to get the size of a JavaScript object ?
In this article, we will see the methods to find the size of a JavaScript object. These are the following ways to solve the problem: Table of Content Using Object.keys() methodUsing Object.objsize() methodUsing Object.entries() methodUsing Object.values() methodUsing Object.keys() methodWe can get t
2 min read
How to Convert String to Array of Objects JavaScript ?
Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read
How to get the Class Name of an Object in JavaScript
In this article, we will learn how we can get the class Name of an Object with multiple approaches in JavaScript. In JavaScript, determining an object's class name can be done using multiple approaches. The constructor property of an object reveals the function that created it, while the instanceof
3 min read
How to read properties of an Object in JavaScript ?
Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to Convert Object to Array in JavaScript?
In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
How to create an object with prototype in JavaScript ?
In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
How to create object properties in JavaScript ?
JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
4 min read
How to get property descriptors of an object in JavaScript ?
Here, we are going to discuss the property descriptors of an Object in JavaScript. The Object.getOwnPropertyDescriptor() method returns an object describing a specific property on a given object. A JavaScript object can be created in many ways and its properties can be called by using property descr
2 min read
How to convert an object to string using JavaScript ?
To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp
4 min read