如何更改对象属性IF的值,并且仅在该值满足特定条件的情况下才能更改? (JavaScript)

时间:2018-09-21 01:03:21

标签: 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 : {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”错误:

    function getLetterCoords(letter)
    {
    coords = [];
    Object.values(tiles).map(tile => 
        {
            if(tile.letter === letter)
                {
                coords['x'] = tile.x;
                coords['y'] = tile.y;
                }
        return coords;
        })
    }

function getLetterFromCoords(coords)
    {
    Object.values(tiles).map(tile => 
        {
            if ((tile.x === coords['x']) && (tile.y === coords['y']))
                {
                return tile.letter;
                }
        })
    }

function encrypt()
    {
    plaintext = document.getElementById('plaintext').value;
    key = document.getElementById('key').value;
    ciphertext = '';

    for (var i = 0; i < plaintext.length; i++)
        {
        plaintextCoords = getLetterCoords(plaintext.charAt(i));
        keyCoords = getLetterCoords(key.charAt(i));
        ciphertext += getLetterFromCoords(keyCoords);
        }

    document.getElementById('ciphertext').value = ciphertext;
    }

3 个答案:

答案 0 :(得分:2)

使用一个简单的循环来检查图块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);

Object.entries返回一个包含所有对象键(也称为属性,属性)及其值的数组。

我的示例是一个纯函数,它是当今JavaScript的#1趋势。没有纯度,它可能会更简单。

答案 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 

答案 2 :(得分:2)

转换为array并使用map并返回更改

var alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
var 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},
};

Object.values(tiles).map(tile => {
  if(tile.letter === 'A') {
    tile.x -= 1;
    tile.y += 1;
  }
  return tile;
})

console.log(tiles);