javascript - 如何在二维数组中使用 Array.push 推送单个值?
问题描述
const y = [...Array(2)].map(() => Array(3).fill([]));
console.log('Before: ', JSON.stringify(y));
// Before: [[[],[],[]],[[],[],[]]]
y[0][1].push("foo");
console.log('After: ', JSON.stringify(y));
// After: [[["foo"],["foo"],["foo"]],[[],[],[]]]
为什么“foo”被插入了三遍?我怎样才能只在第一个中插入“foo”,即我怎样才能得到
// After: [[["foo"],[],[]],[[],[],[]]]
解决方案
推荐阅读
- java - 安卓网络问题
- python - 如何搜索字符串并将数值返回给 C++?
- php - 带有自定义链接的 Moodle 扩展导航用户
- python - 不明白为什么我的 for 循环只迭代一次
- android - REACT-NATIVE:android - SplashScreen 活动未完成?
- c# - 有没有比使用动态 WPF 路径索引更好的选择?
- .net - 关于依赖注入的 Dotnet 问题
- django - 使用 Django 为用户设置登录名
- reactjs - 模块解析失败:使用这些加载器处理了意外的令牌文件:
- angular - 当我使用 Angular 单击以下项目结构的编辑按钮时,如何填充反应式表单?