我正在使用prism-react-renderer
,它突出显示了我在MDX中使用JSX呈现的模板字符串。这会导致在使用它时出现一些空格,原因是间距:
<Code>
{`
function test() {
return "Test";
};
`}
</Code>
这将导致prism-react-renderer
创建一个空的第一行和最后一行。如果这些行是空的,我想删除它们的第一行和最后一行。我使用的数组具有以下类型:
type TokenArray = {
types: string[];
content: string;
empty?: boolean;
}[][];
外部数组包含表示线条的数组。这些内部数组包含一个具有empty
属性的对象。如果该行的空属性设置为true,我希望从外部数组tokenArray
中删除第一项或最后一项。
我试过什么
使用tokenArray[0]
查看外部数组的第一个项,并获取其唯一的项(如何?)并检查该对象是否具有一个名为empty
的属性,其值为true
。如果是这样的话,同样可以对最后一项调用tokenArray.shift()
,但我不知道如何使用tokenArray[?]
访问它。
请帮帮我。
发布于 2020-04-09 03:10:24
如果您想要2d数组中的第一个元素,那么使用like数组。(就像我们正在通过arrayrow访问)
对于最后一项,将是
列最后一行
last_row = arrayarray.length -1
最后一行中的最后一项= arrayarray.length -1
这是打字稿操场的链接
type TokenArray = {
types: string[];
content: string;
empty?: boolean;
}[][];
const tokens: TokenArray = [
[{ types: ['a', 'b'], content: 'abc', empty: true },
{ types: ['c', 'd'], content: 'cde', empty: false }],
[{ types: ['p', 'q'], content: 'pqr', empty: true },
{ types: ['x', 'y'], content: 'xyz', empty: false }],
];
const first = tokens[0][0];
const last = tokens[tokens.length - 1][tokens[tokens.length - 1].length - 1];
console.log('first', first, "Is Empty true?: ", first.empty === true);
console.log('last', last, "Is Empty true?: ", last.empty === true);
https://stackoverflow.com/questions/61119021
复制