最佳答案
在这种情况下我通常使用两个循环:
const results = [];
for(const dx of [-1, 0, 1]) {
for(const dy of [-1, 0, 1]) {
if(dx === 0 && dy === 0) continue;
results.push({ x: x + dx, y: y + dy });
}
}
或者只是:
const neighbours = [
[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], /*0, 0*/ [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]
];
const results = neighbours.map(([dx, dy]) => ({ x: x + dx, y: y + dy }));
关于javascript - 如何在 Javascript 中获取特定坐标 (x, y) 周围的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507608/