Skip to content
Advertisement

Remove index from console.table()

I am viewing an array of data in the console.

console.table(myArray) always has the index as the first column. This is fine when viewing object data, when the index is the key, but not when the index is the array index (in my case it is distracting/ annoying/ takes away from the content). Is there any way to show the table without this index? The optional columns parameter allows one to show only wanted columns… except for the index.

Advertisement

Answer

As shown in the MDN Web docs

The first column in the table will be labeled (index). If data is an array, then its values will be the array indices. If data is an object, then its values will be the property names. Note that (in Firefox) console.table is limited to displaying 1000 rows (first row is the labeled index).

So for an array, you cannot hide the index key to be shown. BUT, as a workaround, you could transform the array into an object where you use your keys.

Example: (Open your console to see results)

const array = [{myId: 42, name: 'John', color: 'red'}, {myId: 1337, name: 'Jane', color: 'blue'}]

const transformed = array.reduce((acc, {myId, ...x}) => { acc[myId] = x; return acc}, {})

console.table(transformed)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement