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]);
}
});
});
CodePudding user response:
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}
CodePudding user response:
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:
- Most efficient way to concatenate strings in JavaScript?
- How can I process each letter of text using Javascript?
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.