0

I tried to join and add line break to my nested array using join and \n as you can see:

const exampleArray = [["Hello"], ["world"], ["example"]]
    .map((el) => el)
    .join("\n");

everything works. I get this output which is what I want:

Hello

world

example

but when I tried to add this exampleArray to a value inside of an object, it shows the \n itself instead of line break.

this is what I did:

const theObj = {
    value: exampleArray,
  };

and this is what I get in the output:

Object { value: "Hello\nworld\nexample" }

I want to have my text with line breaks in value. what is the problem and how can I solve it?

3
  • 1
    There are line breaks there. theObj.value and exampleArray are identical. Commented Dec 25, 2022 at 6:44
  • 1
    why don't you use theObj.value? Commented Dec 25, 2022 at 6:46
  • ok I found out. thanks Commented Dec 25, 2022 at 6:49

2 Answers 2

1

Here the problem is you are using objects where the data is stored in form of {key : string} and {value : <number | string | boolean >} form. You can not possibly store visual line-breaks in object-values. But if you print Object.value rather than Object itself it will print data in visual line breaks.

const obj = {
  value : 'Hindi\nEnglish\nFrance'
}
console.log(obj)
console.log(obj.value);


/* first one will give o/p - {
  "value": "Hindi\nEnglish\nFrance"
} */

/* second one will give o/p 
Hindi
English
France */

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

Comments

1

You have text with line breaks in value. If you print in console theObj.value you will see expected.

Comments

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.