I'm having a little trouble with a code. I have a list of words that need to be separated and parsed instead of rows in columns.
This code actually works very well and does that job. How is actually working:
const Obj = {
"0":"Mario, Louigi, Peach",
"1":"Wario, King Kong, Bomberman",
"2":"Dracula, Doctor Z, Raikou"
};
const Obj3 = [];
var count = Obj[0].split(", ").length;
var countOuter = Object.keys(Obj).length;
for( var i = 0; i < count; i++){
var string = [];
for( var j = 0; j < countOuter; j++){
string.push(Obj[j].split(", ")[i]);
}
Obj3[i] = string;
}
console.log(Obj3);
But if any name has a comma in the middle example: Mr, Robot it considers that comma as a separator comma and divides Mr, Robot in two instead of one.
Example:
const Obj = {
"0":"Mr, Robot, Louigi, Peach",
"1":"Wario, King Kong, Bomberman",
"2":"Dracula, Doctor Z, Raikou"
};
const Obj3 = [];
var count = Obj[0].split(", ").length;
var countOuter = Object.keys(Obj).length;
for( var i = 0; i < count; i++){
var string = [];
for( var j = 0; j < countOuter; j++){
string.push(Obj[j].split(", ")[i]);
}
Obj3[i] = string;
}
console.log(Obj3);
And then it creates an undefined record that causing me errors. Any idea how to solve this? Thanks in advance!!
If you can't get the names before they've been joined by commas, you might be able to use a regex delimiter to split them. For example, if it would suffice to not split at commas preceded by only two non-whitespace characters, you could use:
.split(/(?<=[^ ,]{3,}), /)
This regex will match ', '
only if it is preceded by at least 3 characters that don't match ' '
or ','
.