Sudheer Kandula
Sudheer Kandula

Reputation: 11

convert array of letters to array of words using JavaScript

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"

Upvotes: 1

Views: 1051

Answers (6)

Braeden Lisowski
Braeden Lisowski

Reputation: 87

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

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

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

Upvotes: 0

Mudassar H.
Mudassar H.

Reputation: 76

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.

Upvotes: 0

BadPiggie
BadPiggie

Reputation: 6359

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);

Upvotes: 0

bui
bui

Reputation: 1651

You can use reduce

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

Upvotes: 0

mksmanjit
mksmanjit

Reputation: 244

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);

Upvotes: 2

nullptr
nullptr

Reputation: 4484

You want to use

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

Note the difference between '' and ' '.

Upvotes: 1

Related Questions