如何在 console.table 中显示对象属性

问题描述 投票:0回答:1

我想在表格中显示字符属性,我该怎么做?它当前记录整个对象并且在我使用 console.table(tileGridArray.char) 时未定义

    const tileGridArray = [];


for (let rowIndex = 0; rowIndex < 3; rowIndex++) { // Loop through each row
    const row = [];

    for (let colIndex = 0; colIndex < 3; colIndex++) { // Loop through each column in the current row
        const tileObj = {
        row: rowIndex,
        col: colIndex,
        type: null, // mine true of false
        visibility: null,
        char: 'why'
    };

    row.push(tileObj);
    }

    tileGridArray.push(row); }


console.table(tileGridArray)
javascript arrays javascript-objects
1个回答
0
投票

要在表格中只显示

char
属性,可以创建一个新数组,只包含
tileGridArray
中每个对象的char属性,然后将该数组传递给
console.table()

const charArray = tileGridArray.map(row => row.map(tileObj => tileObj.char));
console.table(charArray);

© www.soinside.com 2019 - 2024. All rights reserved.