将数组推入TS中的数组

发布于 2025-02-09 07:50:57 字数 327 浏览 1 评论 0原文

我来自Python,并且并不完全熟悉“适当”的JS/TS做事方式。

我正在浏览一组元素,然后将其中一些元素的列表推向2D数组。

let res: number[][];
for (let posElement of posSet) {
    if (negSet.has(-1*posElement)) {
        res.push([-1*posElement, 0, posElement]);
    }
}

我收到以下错误typeError:无法读取未定义的属性(读取'push')。我在做什么错,我在哪里弄乱语法?

I am coming from Python, and am not entirely familiar with the 'proper' JS/TS way of doing things.

I am looping through the elements of a set, and I am pushing lists of some of the elements onto a 2D array.

let res: number[][];
for (let posElement of posSet) {
    if (negSet.has(-1*posElement)) {
        res.push([-1*posElement, 0, posElement]);
    }
}

I am getting the following error TypeError: Cannot read properties of undefined (reading 'push'). What am I doing wrong, where am I messing up the syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2025-02-16 07:50:58

像这样的初始化

let res: number[][]=[];

like this initialize

let res: number[][]=[];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文