Home > front end >  convert array of letters to array of words using JavaScript
convert array of letters to array of words using JavaScript

Time:06-27

convert array of letters to array of words using JavaScript

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ]; and my desired output is : output = ['bake','cake','eat'];

I used Join method but I am not getting desired output. const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];

    const result=inputArr.join('');
    console.log(result);

output: "bakecakeeat"

CodePudding user response:

If you want the answer in different way other than Join.

You can iterate over the inputArr then keep appending the character until you found the empty space(''), there you can add the word in the outputArr and and reset the word.

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];
var outputArr = [];
var word = '';
// Iterate over the array.
for (let i = 0; i < inputArr.length; i  ) {
  if(inputArr[i]=='') {
    // If you found the empty Space(''), add to the outputArr
    outputArr.push(word);
    // reset the word, to start making the new word.
    word = '';
    // No need to execute the below code, so continue.
    continue;
  }
  // Keep appending the character to the word.
  word = word.concat(inputArr[i]);
}
// This is for the last word string.
if(word != '') {
 outputArr.push(word);
}
console.log(outputArr);

CodePudding user response:

You want to use

const inputArr = [ 'b', 'a', 'k', 'e', ' ', 'c', 'a', 'k', 'e', ' ', 'e', 'a', 't' ];

Note the difference between '' and ' '.

CodePudding user response:

If you do not have control over the input you can change the array to fit the other answer's method:

inputArr.forEach((element, index) => {
  if(element === '') {
    inputArr[index] = ' ';
  }
});
inputArr.join('');

CodePudding user response:

You can use reduce

inputArr.reduce((prev,curr) => {
    if (curr === '') {
        prev.push('');
    } else {
        prev[prev.length - 1]  = curr;
    }
    return prev;
}, [''])

CodePudding user response:

If you have control over the input, You should replace '' with ' ', Then can do the following,

const inputArr = [ 'b', 'a', 'k', 'e', ' ', 'c', 'a', 'k', 'e', ' ', 'e', 'a', 't' ];
const output = inputArr.join('').split(' ');
console.log(output);

IF NOT You can follow this,

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];
const output = inputArr.map( letter => letter === '' ? ' ':letter).join('').split(' ');
console.log(output);

CodePudding user response:

According to your given array, you want to join the letters and return an array of words, correct me if I am wrong.

So here is my solution for a given problem.

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];

let result = inputArr.map(item => item === '' ? ' ' : item).join('').split(' ');

console.log(result);

If you don't have control over input, then you can simply replace '' with ' ' and split it, period.

  • Related