I'm new here on the site, I have a very simple problem with JS, but I'm unable to resolve it.
I have an array of strings: something like this:
arr = [
"Economic_Working",
"Health_Excursions",
"Health_Doctors",
"Health_Meditation",
"Social_Religion",
"Social_Holidays"
]
I want to become 2 such arrays:
categoryArr = ["Economic" , "Health" , "Social" ];
subCategoryArr = [
["Working"] ,
[ "Meditation" , "Doctors" , "Excursions"] ,
[ "Religion" , "Holidays"]
];
The first array contains the first part of the text, And in the second array, I put the second part but according to the index which is of its category which is in the first array
this is what i try to do:
let arr = [
"Economic_Working",
"Health_Excursions",
"Health_Doctors",
"Health_Meditation",
"Social_Religion",
"Social_Holidays"
]
let convertTo2Arrays = (array) => {
let categoryArr = []
array.map(item => {
firstPart = item.split("_")[0];
return categoryArr.push(firstPart)
})
return categoryArr;
}
console.log(convertTo2Arrays(arr))