1
const twoD = [
'rnbqkbnr', 'pppppppp',
'8',        '8',
'4P3',      '8',
'PPPP1PPP', 'RNBQKBNR'
]

I have a 2d array that looks like this How can I go about replacing every single number with an equal amount of characters:

Expected output:

[
'rnbqkbnr', 'pppppppp',
'oooooooo', 'oooooooo',
'ooooPooo', 'oooooooo',
'PPPPoPPP', 'RNBQKBNR'
]

Tried this code, however, recieved this error:

TypeError: Cannot assign to read only property '0' of string 'rnbqkbnr'

twoD.map((row, i) => {
    row.split("").map((col, j) => {
        if (isNaN(twoD[i][j])) {
            twoD[i][j] = "o".repeat(twoD[i][j]);
        }
    });
});
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
  • Tried this code, however, recieved this error: ```TypeError: Cannot assign to read only property '0' of string 'rnbqkbnr'``` ```twoD.map((row, i) => { row.split("").map((col, j) => { if(isNaN(twoD[i][j])){ twoD[i][j] = "o".repeat(twoD[i][j]); } } );});``` – CharlieS1103 Mar 14 '22 at 05:13
  • Those are array methods, you've to convert the string to an array first. Please add your code to the question, then we can see what goes wrong. – Teemu Mar 14 '22 at 05:14

2 Answers2

1

It is actually a 1D array until you split its elements into symbols.

const data = ['rnbqkbnr', 'pppppppp', '8', '8', '4P3', '8', 'PPPP1PPP', 'RNBQKBNR'];
const result = data.map(row => row.split('')
  .map(char => isNaN(char) ? char : 'o'.repeat(char))
  .join(''));
    
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
  • Thanks! Worked for me, for those interested, I was attempting to convert A FEN(chess notation system) into a 2d Array: Code to do so below ```twoD = this.state.chess.fen().split(" ")[0].split("/"); console.log(twoD); let gameBoardMap = twoD.map(el => el.split("").map(c => !isNaN(c) ? "o".repeat(c) : c).join("")); // Make gameBoardMap a 2d array gameBoardMap = gameBoardMap. ``` – CharlieS1103 Mar 14 '22 at 06:02
0

If the character you want to use to replace your numbers is fixed, then you could use Array.map to do something like this:

const inputArr = [
'rnbqkbnr', 'pppppppp',
'8',        '8',
'4P3',      '8',
'PPPP1PPP', 'RNBQKBNR'
]; // given example input

const replChar = "o"; // character to put in place of numbers

let outputArr = inputArr.map((inputStr) => {
   // passing empty string as the delimiter results in an array of characters.
   let charArray = inputStr.split("");
   let newChars = charArray.map((character) => {
      // loop over every character and return a string
      const nums = "1234567890";
      if(nums.includes(character)) {
         // replace numbers with n letters.
         let number = parseInt(character);
         return replChar.repeat(number);
      }
      return character;
   });
   return newChars.join(""); // merge into string
});

You could also do this with a for loop:

const inputArr = [
'rnbqkbnr', 'pppppppp',
'8',        '8',
'4P3',      '8',
'PPPP1PPP', 'RNBQKBNR'
]; // given example input

const replChar = "o"; // character to put in place of numbers

let outputArr = [];
for(var i = 0; i < inputArr.length; i++) {
   let inputStr = inputArr[i];
   // passing empty string as the delimiter results in an array of characters.
   let charArray = inputStr.split("");
   let newChars = "";
   for(var j = 0; j < charArray.length; j++) {
      let character = charArray[j];
      // loop over every character and return a string
      const nums = "1234567890";
      if(nums.includes(character)) {
         // replace numbers with n letters.
         let number = parseInt(character);
         newChars += replChar.repeat(number);
      } else {
         newChars += character;
      }
   }
   outputArr.push(newChars)
}

I also want to mention that when it comes to scaling these solutions, there may be possible improvements. If performance matters to you on this problem, it may be worth some time looking into some of the following posts and potentially doing your own benchmarks:


Towards the error you received, it looks like you are trying to index into a string which would return the character from that position in read-only mode because Javascript strings are immutable. You need to build new strings instead based on your original strings.
fraz
  • 26
  • 4