How could I join a string inside an array of array? I’m trying to remove the space between inside array.
This is the array sample.
[
[ 'time' ],
[ 'user full name' ],
[ 'affected user' ],
[ 'event context' ],
[ 'component' ],
[ 'event name' ],
[ 'description' ],
[ 'origin' ],
[ 'ip address' ]
]
I’m trying this to become like this.
[
[ 'time' ],
[ 'userfullname' ],
[ 'affecteduser' ],
[ 'eventcontext' ],
[ 'component' ],
[ 'eventname' ],
[ 'description' ],
[ 'origin' ],
[ 'ipaddress' ]
]
This is my function i’m trying to run
const MakeArrayToSmall = (headerRow) => {
let arrayToCheck = [];
for (let i = 0; i < headerRow.length; i++) {
let lowercaseLetter = headerRow[i].toLowerCase().split();
arrayToCheck.push(lowercaseLetter)
}
return arrayToCheck
};
const samplearray = MakeArrayToSmall(headerRow)
console.log(samplearray)
4
Answers
You will need to split the string with a space character as a delimiter and then join the resulting array without any delimiter to remove the spaces.
A shorter version of the working code:
Loop the
headerRow
and then select the first element in the inner array and replace whitespaces with empty stringheaderRow[i][0].toLowerCase().replace(/s/g, "")
Here is a code.
Explanation:
Since there is array inside an array. map traverses the outer array and provides access to inside array each time .the inside array has only one element we can access it via
e[0]
then we perform replace using regex.For more information you can put console statements whereever you find its hard to understand.
Use flatMap method.
example:
only one level is flattened:
arr1.flatMap((x) => [[x * 2]]) will return your two dimensional array;