1

I want to convert this array into string includes space elements in javascript. space doesn't show after return.

let results = ['Hello',' ',' ',' ','EveryOne',' ',' ',' ','My',' ','Name',' ','is',' ','X'];

const result = () => 
{
    for(let i = 0; i<results.length; i++)
    {
        return results;
    } 
 } 

result();

// Expected Output : "Hello   EveryOne   My  Name is X";
3
  • 1
    Perhaps also show expected output in your question. Because if you want duplicitous spaces then thats not all clear Commented Feb 7, 2021 at 10:14
  • Ok i see you show expected out put. You can use standard array function for join Commented Feb 7, 2021 at 10:15
  • Your code has syntax errors which make it hard to help you. Please fix the syntax errors. But the short answer is: Just use join(""). That will not remove the spaces. Commented Feb 7, 2021 at 10:15

5 Answers 5

4

use Array.prototype.join with empty string as separator:

const result = () => {
    let results = ['Hello',' ',' ',' ','EveryOne',' ',' ',' ','My',' ','Name',' ','is',' ','X'];
    return results.join('')
 } 
 
 console.log(result())

Sign up to request clarification or add additional context in comments.

1 Comment

If you dont care about repetitive whitespace as seems to be the case, then here is the solution @SilentStorm
2

You need to concatenate the elements in the for-loop:

const result = (results=[]) => {
  let str = '';
  for(let i = 0; i < results.length; i++) {
    str += results[i];
  } 
  return str;
} 
 
console.log( result(['Hello',' ',' ',' ','EveryOne',' ',' ',' ','My',' ','Name',' ','is',' ','X']) );

Another way using .join:

const result = (results=[]) => {
  return results.join('');
} 
 
console.log( result(['Hello',' ',' ',' ','EveryOne',' ',' ',' ','My',' ','Name',' ','is',' ','X']) );

1 Comment

But OP seems to want the repetitive white space where it occurs. Scratching my head on why but seems to be the desired result on even if two or more spaces included in the string result between tokens
1

Since reactjs tag is there, am assuming issue rendering. Use HTML entity   for ' ', that should display correct as in snippet.

const Component = () => {
  let results = [
    "Hello",
    " ",
    " ",
    " ",
    "EveryOne",
    " ",
    " ",
    " ",
    "My",
    " ",
    "Name",
    " ",
    "is",
    " ",
    "X",
  ];
  return (
    <div>
      {" "}
      {results.map((word) => (
        word === ' ' ? <span>&nbsp;</span> : <span> { word } </span>
      ))}{" "}
    </div>
  );
};

ReactDOM.render(<Component />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>


<div id="app"> </div>

Comments

1

You can convert your array to string, and then split commas per something like: results.toString().split(',').join('');

Comments

0
 let result = arr.filter(item => {
    if (item !== " "){
        return item
    };
  });
  console.log("result",result)

2 Comments

It's good practice on StackOverflow to add an explanation as to why your solution should work.
Answer's are great. But for best practices, please provide an explanation. You only posting code makes the OP and future commers copy and paste your answer without understanding the logic behind the answer. Please provide an answer with some explanation. Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.