How do I convert this array :
let strArr = ["10100", "10111", "11111", "01010"];
into a 2-d array.
The 2-d array will be :-
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
0 1 0 1 0
You could get the iterables from string and map numbers.
var array = ["10100", "10111", "11111", "01010"],
matrix = array.map(s => Array.from(s, Number));
console.log(matrix);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Array.from iterates over the characters of one of the strings, and then calls Number on each and returns the results as a new array, effectively turning "10010" into [1, 0, 0, 1, 0]If the result needs to be an array of array containing one element per character of a string, you can split each string with an empty string in a loop or map.
let res = [];
let strArr = ["10100", "10111", "11111", "01010"];
strArr.map((str) => {
res.push(str.split(''))
});
console.log(res);
let strArr = ["10100", "10111", "11111", "01010"];
let numColumns = strArr[0].length;
let numRows = strArr.length;
let string = strArr.join('');
let result = [];
for (let row = 0; row < numRows; row++) {
result.push([]);
for (let column = 0; column < numColumns; column++) {
result[row].push(parseInt(string.charAt(row*5 + column), 10));
}
}
console.log(result);
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 1 0 1 0a 2d array?