DEV Community

Cover image for Usa console.table en lugar de console.log
Fernando Barrios - jfergt
Fernando Barrios - jfergt

Posted on • Originally published at jfbarrios.com

1 1

Usa console.table en lugar de console.log

Una forma interesante de mostrar los resultados de un array o un objeto es utilizar console.table. Esta función toma un argumento obligatorio: data, que debe ser un array o un objeto, y un parámetro adicional: columns.

Colecciones de tipos primitivos

// Array de string
console.table(['Manzana', 'Pera', 'Melón']);
Enter fullscreen mode Exit fullscreen mode

image.png

// Objeto con propiedades de tipo strings
function Persona(nombres, apellidos) {
  this.nombres = nombres;
  this.apellidos = apellidos;
}

var yo = new Persona("Fernando", "Barrios");

console.table(yo);
Enter fullscreen mode Exit fullscreen mode

image.png

Colecciones de tipos compuestos

Si data es un array y sus elementos son array, o bien cuando data sea un objeto y sus propiedades sean un array sus propiedades o elementos se enumerarán en la fila.

// un array de arrays

var personas = [["Fernando", "Barrios"], ["Juan", "Carlos"], ["Carmen", "María"]]
console.table(personas);
Enter fullscreen mode Exit fullscreen mode

image.png

// un array de objetos

console.table([{
    nombre: "Fernando",
    apellido: "Barrios"
}, {
    nombre: "John",
    apellido: "Doe"
}])
Enter fullscreen mode Exit fullscreen mode

image.png

Restringiendo las columnas mostradas

// un array de objetos, donde se mostrará solo la columna apellido

console.table([{
    nombre: "Fernando",
    apellido: "Barrios"
}, {
    nombre: "John",
    apellido: "Doe"
}], ['apellido'])
Enter fullscreen mode Exit fullscreen mode

image.png

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay