sajad torkamani

Object.entries() takes an object as an argument and returns an array with the same length as the number of properties in the object where each array item has two elements and corresponds to a property in the object:

  • [0]: The key of the object.
  • [1]: The value of the key in the object.

For example:

const book = {
  title: "The Great Gatsby",
  author: "F. Scott Fitzgerald",
  yearPublished: 1925,
  genre: "Fiction",
  pages: 180,
  reviews: [
    { reviewer: "John Doe", rating: 5, comment: "A timeless classic." },
    { reviewer: "Jane Smith", rating: 4, comment: "Beautifully written." }
  ],
  publisher: {
    name: "Charles Scribner's Sons",
    location: "New York",
    year: 1925
  }
};

console.log(Object.entries(book));

Will return:

[
  ["title", "The Great Gatsby"],
  ["author", "F. Scott Fitzgerald"],
  ["yearPublished", 1925],
  ["genre", "Fiction"],
  ["pages", 180],
  ["reviews", [
    { reviewer: "John Doe", rating: 5, comment: "A timeless classic." },
    { reviewer: "Jane Smith", rating: 4, comment: "Beautifully written." }
  ]],
  ["publisher", {
    name: "Charles Scribner's Sons",
    location: "New York",
    year: 1925
  }]
]
Tagged: JavaScript

Leave a comment

Your email address will not be published. Required fields are marked *