javascript - 使用包含矢量坐标的另一个对象的对应键更改 2D 数组键名称
问题描述
目标:用每个包含 (x, y) 坐标的表格单元格初始化一个空白棋盘,然后通过用适当的棋子重命名密钥来更新棋盘的相应单元格来填充初始棋子。坐标(键值)将保持不变。
问题:使用 map 函数用适当的棋子更改空白棋盘的键名时遇到问题。
1) **typeError** from pushing Vector object to an array
2) **undefined** returned from nested map function
经过研究,更改密钥的一种方法是使用
Object.keys(myObject).map(key => ({[key]: myObject[key]}));
或者
Object.entries(myObject).map(([p, v]) => ({[p]: v}))
但是,当map函数为 2D 数组嵌套时,我无法让它工作。
const BOARD = `
wbwbwbwb
bwbwbwbw
wbwbwbwb
bwbwbwbw
wbwbwbwb
bwbwbwbw
wbwbwbwb
bwbwbwbw
`;
const PIECES = `
rkbqibkr
pppppppp
--------
--------
--------
--------
pppppppp
rkbqibkr
`;
//class Board
static initalize(board, pieces){
let boardCoords = board.coordinates;
let pieceCoords = pieces.trim().split("\n").map(l => [...l]);
let initialPieces = [];
let startBoard;
//this is to add the appropriate coordinates of where each chess piece
//is at
//the parameter 'board' that was passed is an instance of the Board
//class, where each cell had the appropriate (x, y) coordinates. The
//code was exactly the same as below, and it worked. Not sure why it's
//not working for below. OUTPUTS: typeError
this.initialPieces = pieceCoords.map((row, y) => {
return row.map((ch, x) => {
if(ch !== '-'){
this.initialPieces.push(new Vec(x, y));
}
});
});
this.startBoard = Object.entries(boardCoords).map(row, y => {
key.map(ch, x => {
if(ch !== "-"){
({ [ch]: boardCoords[y][x]});
}
});
});
return startBoard;
}}
[试图将向量推入数组]
未捕获的类型错误:无法读取未定义的属性“推送”
[尝试使用 .map(item, item2) 进行映射]
未捕获的 ReferenceError:未定义行
[尝试使用 .map(item) 进行映射]
未捕获的类型错误:无法读取未定义的属性“0”
[预期结果]
[0]: ["p"]: Vec (x, y), ...
解决方案
推荐阅读
- r - 使用测试统计数据(均值、标准差、计数)在 R 中进行独立样本 t 检验
- azure - SSIS:如何枚举 Azure 存储中的文件并将每个文件加载到 SQL Server
- java - Nginx 反向代理只允许经过身份验证的用户使用 Spring Boot api
- php - 从 PHP 中的 JSON 数组中获取随机项
- sql-server - MERGE 语句包含多部分标识符
- json - 标头授权不存在
- c# - CsvHelper:如何忽略标题行?
- node.js - 应用程序机器人 Discord.js
- amazon-web-services - AWS 实时监控资源,
- linux - 使用 Vim 替换 .so 文件中的文本