当且仅当对象属性的值满足特定条件时,如何更改该属性的值?(Javascript)
对于动态Polybius正方形,我有以下代码:当且仅当对象属性的值满足特定条件时,如何更改该属性的值?(Javascript),javascript,Javascript,对于动态Polybius正方形,我有以下代码: /* 1 2 3 4 5 1 A B C D E 2 F G H I K 3 L M N O P 4 Q R S T U 5 V W X Y Z */ alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ'; tiles = { A : {letter: alphabet.charAt(0), x: 1, y: 1}, B : {letter: alphabet.charAt(1), x: 2, y: 1}, C : {
/*
1 2 3 4 5
1 A B C D E
2 F G H I K
3 L M N O P
4 Q R S T U
5 V W X Y Z
*/
alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
tiles = {
A : {letter: alphabet.charAt(0), x: 1, y: 1},
B : {letter: alphabet.charAt(1), x: 2, y: 1},
C : {letter: alphabet.charAt(2), x: 3, y: 1},
D : {letter: alphabet.charAt(3), x: 4, y: 1},
E : {letter: alphabet.charAt(4), x: 5, y: 1},
F : {letter: alphabet.charAt(5), x: 1, y: 2},
G : {letter: alphabet.charAt(6), x: 2, y: 2},
H : {letter: alphabet.charAt(7), x: 3, y: 2},
I : {letter: alphabet.charAt(8), x: 4, y: 2},
K : {letter: alphabet.charAt(9), x: 5, y: 2},
L : {letter: alphabet.charAt(10), x: 1, y: 3},
M : {letter: alphabet.charAt(11), x: 2, y: 3},
N : {letter: alphabet.charAt(12), x: 3, y: 3},
O : {letter: alphabet.charAt(13), x: 4, y: 3},
P : {letter: alphabet.charAt(14), x: 5, y: 3},
Q : {letter: alphabet.charAt(15), x: 1, y: 4},
R : {letter: alphabet.charAt(16), x: 2, y: 4},
S : {letter: alphabet.charAt(17), x: 3, y: 4},
T : {letter: alphabet.charAt(18), x: 4, y: 4},
U : {letter: alphabet.charAt(19), x: 5, y: 4},
V : {letter: alphabet.charAt(20), x: 1, y: 5},
W : {letter: alphabet.charAt(21), x: 2, y: 5},
X : {letter: alphabet.charAt(22), x: 3, y: 5},
Y : {letter: alphabet.charAt(23), x: 4, y: 5},
Z : {letter: alphabet.charAt(24), x: 5, y: 5},
};
在某一点上,我想交换行和列。例如,如果我想交换第1行和第2行,我会将x==1的每个磁贴的x值更改为2,反之亦然
我怎么能这么做
编辑:
第一个答案的代码是“无法读取未定义的属性x”错误:
函数getLetterCoords(字母)
{
坐标=[];
Object.values(tiles.map)(tile=>
{
if(tile.letter==字母)
{
坐标['x']=tile.x;
坐标['y']=tile.y;
}
返回坐标;
})
}
函数getLetterFromCoords(coords)
{
Object.values(tiles.map)(tile=>
{
如果((tile.x==coords['x'])&&(tile.y==coords['y']))
{
回信;
}
})
}
函数encrypt()
{
明文=document.getElementById('plaintext')。值;
key=document.getElementById('key').value;
密文=“”;
for(var i=0;ifunction swapRows(tiles, y1, y2) {
const newTiles = {};
for (const [key, tile] of Object.entries(tiles)) {
// If the tile is from the first row, change its row to the second
if (tile.y === y1) {
newTiles[key] = {...tile, y: y2};
continue;
}
// If the tile is from the second row, change its row to the first
if (tile.y === y2) {
newTiles[key] = {...tile, y: y1};
continue;
}
// Otherwise don't change
newTiles[key] = tile;
}
return newTiles;
}
const swappedTiles = swapRows(tiles, 1, 2);
返回包含所有对象键(也称为属性、属性)及其值的数组
我的例子是一个纯函数,它是当今JavaScript的#1趋势。如果没有纯度,它可能会更简单。使用一个简单的循环来检查磁贴X或Y并更改它:
function swapRows(tiles, y1, y2) {
const newTiles = {};
for (const [key, tile] of Object.entries(tiles)) {
// If the tile is from the first row, change its row to the second
if (tile.y === y1) {
newTiles[key] = {...tile, y: y2};
continue;
}
// If the tile is from the second row, change its row to the first
if (tile.y === y2) {
newTiles[key] = {...tile, y: y1};
continue;
}
// Otherwise don't change
newTiles[key] = tile;
}
return newTiles;
}
const swappedTiles = swapRows(tiles, 1, 2);
// Convert to array, then go through each
Object.values(tiles).forEach(tile => {
// do what ever check you want in here
if(tile.letter === 'W') {
tile.x = 0;
tile.y = 0;
}
})
console.log(tiles); // Will now show the change
返回包含所有对象键(也称为属性、属性)及其值的数组
我的例子是一个纯函数,它是当今JavaScript的#1趋势。如果没有纯度,它可能会更简单。转换为
数组
,使用映射
并返回更改
// Convert to array, then go through each
Object.values(tiles).forEach(tile => {
// do what ever check you want in here
if(tile.letter === 'W') {
tile.x = 0;
tile.y = 0;
}
})
console.log(tiles); // Will now show the change
var字母表='ABCDEFGHIKLMNOPQRSTUVWXYZ';
可变磁砖={
A:{字母:字母表。字符(0),x:1,y:1},
B:{字母:字母表。字符(1),x:2,y:1},
C:{字母:字母表。字符(2),x:3,y:1},
D:{字母:字母表。字符(3),x:4,y:1},
E:{字母:字母表。字符(4),x:5,y:1},
F:{字母:字母表。字符(5),x:1,y:2},
G:{字母:字母表。字符(6),x:2,y:2},
H:{字母:字母表。字符(7),x:3,y:2},
I:{字母:字母表。字符(8),x:4,y:2},
K:{字母:字母表。字符(9),x:5,y:2},
L:{字母:字母表。字符(10),x:1,y:3},
M:{字母:字母表。字符(11),x:2,y:3},
N:{字母:字母表。字符(12),x:3,y:3},
O:{字母:字母表。字符(13),x:4,y:3},
P:{字母:字母表。字符(14),x:5,y:3},
Q:{字母:字母表。字符(15),x:1,y:4},
R:{字母:字母表。字符(16),x:2,y:4},
S:{字母:字母表。字符(17),x:3,y:4},
T:{字母:字母表。字符(18),x:4,y:4},
U:{字母:字母表。字符(19),x:5,y:4},
V:{字母:字母表。字符(20),x:1,y:5},
W:{字母:字母表。字符(21),x:2,y:5},
X:{字母:字母表。字符(22),X:3,y:5},
Y:{字母:字母表。字符(23),x:4,Y:5},
Z:{字母:字母表。字符(24),x:5,y:5},
};
Object.values(tiles.map)(tile=>{
如果(tile.letter=='A'){
瓷砖x-=1;
y+=1;
}
返回瓷砖;
})
控制台日志(瓷砖)代码>转换为数组
并使用映射
返回更改
var字母表='ABCDEFGHIKLMNOPQRSTUVWXYZ';
可变磁砖={
A:{字母:字母表。字符(0),x:1,y:1},
B:{字母:字母表。字符(1),x:2,y:1},
C:{字母:字母表。字符(2),x:3,y:1},
D:{字母:字母表。字符(3),x:4,y:1},
E:{字母:字母表。字符(4),x:5,y:1},
F:{字母:字母表。字符(5),x:1,y:2},
G:{字母:字母表。字符(6),x:2,y:2},
H:{字母:字母表。字符(7),x:3,y:2},
I:{字母:字母表。字符(8),x:4,y:2},
K:{字母:字母表。字符(9),x:5,y:2},
L:{字母:字母表。字符(10),x:1,y:3},
M:{字母:字母表。字符(11),x:2,y:3},
N:{字母:字母表。字符(12),x:3,y:3},
O:{字母:字母表。字符(13),x:4,y:3},
P:{字母:字母表。字符(14),x:5,y:3},
Q:{字母:字母表。字符(15),x:1,y:4},
R:{字母:字母表。字符(16),x:2,y:4},
S:{字母:字母表。字符(17),x:3,y:4},
T:{字母:字母表。字符(18),x:4,y:4},
U:{字母:字母表。字符(19),x:5,y:4},
V:{字母:字母表。字符(20),x:1,y:5},
W:{字母:字母表。字符(21),x:2,y:5},
X:{字母:字母表。字符(22),X:3,y:5},
Y:{字母:字母表。字符(23),x:4,Y:5},
Z:{字母:字母表。字符(24),x:5,y:5},
};
Object.values(tiles.map)(tile=>{
如果(tile.letter=='A'){
瓷砖x-=1;
y+=1;
}
返回瓷砖;
})
控制台日志(瓷砖)代码>所提供的答案是否有问题?是的!我刚刚接受了一个答案。我相信其他人也能用,但我现在正试着用第一个。提供的答案有什么好运气吗?是的!我刚刚接受了一个答案。我相信其他人也能用,但我现在正试着用第一个。