skip to Main Content

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


  1. 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.

    // ...rest of your code
    let lowercaseLetter = headerRow[i][0].toLowerCase()
                                         .split(' ') // <--- split on space
                                         .join(''); // <-- join
    

    A shorter version of the working code:

    const MakeArrayToSmall = (headerRow) => {
      return headerRow.map(item => [item[0].toLowerCase().split(' ').join('')]);
    };
    
    const headerRow = [
      [ 'time' ],
      [ 'user full name' ],
      [ 'affected user' ],
      [ 'event context' ],
      [ 'component' ],
      [ 'event name' ],
      [ 'description' ],
      [ 'origin' ],
      [ 'ip address' ]
    ];
    
    const samplearray = MakeArrayToSmall(headerRow);
    console.log(samplearray);
    Login or Signup to reply.
  2. Loop the headerRow and then select the first element in the inner array and replace whitespaces with empty string headerRow[i][0].toLowerCase().replace(/s/g, "")

    const MakeArrayToSmall = (headerRow) => {
            let arrayToCheck = [];
    
            for (let i = 0; i < headerRow.length; i++) {
              let lowercaseLetter = headerRow[i][0].toLowerCase().replace(/s/g, "");
              arrayToCheck.push(lowercaseLetter)
            }
            return arrayToCheck
          
          };
    
    Login or Signup to reply.
  3. 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.

    let x = [
      [ 'time' ],
      [ 'user full name' ],
      [ 'affected user' ],
      [ 'event context' ],
      [ 'component' ],
      [ 'event name' ],
      [ 'description' ],
      [ 'origin' ],
      [ 'ip address' ]
    ]
    let z= x.map(e=> {
        let y = e[0].replace(/s/g,'')
        return [y];
    })
    
    console.log(z)
    Login or Signup to reply.
  4. Use flatMap method.

    example:

    const arr = [
      [ 'time' ],
      [ 'user full name' ],
      [ 'affected user' ],
      [ 'event context' ],
      [ 'component' ],
      [ 'event name' ],
      [ 'description' ],
      [ 'origin' ],
      [ 'ip address' ]
    ]
    const result = arr.flatMap(x => [[x[0].toLowerCase().replace(/s/g, "")]])
    console.log("result", result)
    

    only one level is flattened:
    arr1.flatMap((x) => [[x * 2]]) will return your two dimensional array;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search