-1

So I have an array of strings(paragraphs) and the first phrase in each string represents the header. For example...

[
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a 
  witch and a wardrobe. The End"
]

As you can see, the header is separated by a line break \n\. Is there a method I can use to grab the part of the string before the line break and store it in a variable?

I have a large number of these strings and so I'm looking for a solution that isn't just matching "The Lion, the Witch and the Wardrobe"

Thanks in advance guys.

Outcome I need...

let headers = [ "The Lion, the Witch and the Wardrobe", "example", "example" ]

let body = ["The was once a...", "example", "example"]

4 Answers4

2

You can split the string, and then extract the first element like so:

const phrase = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End"
]

const getHeader = ([header, ...rest]) => header

const result = getHeader(phrase[0].split('\n'))

console.dir(result)

After your edit, I see that you also want the body, which you can do like so:

const phrase = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End"
]

const getHeader = ([header, ...body]) => ({
  header: [header],
  body,
})

const result = getHeader(phrase[0].split('\n'))

console.dir(result)

With additional guesswork:

It seems from your question that you have an array of texts and would like two arrays output from each; an array of headers, and an array of bodies. Here's how that might look:

const texts = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End",
  "The Bible\n\In the beginning, God created the heavens and earth"
]

const getHeaderAndBody = ([header, ...body]) => ({
  header: [header],
  body,
});

const mergeHeadersAndBodies = (prev, curr) => ({
  headers: [
    ...(prev.headers || []),
    ...curr.header
  ],
  bodies: [
    ...(prev.bodies || []),
    ...curr.body
  ]
})
  
const splitToNewLines = text => text.split('\n')

const mapToHeadersAndBodies = input => input
  .map(splitToNewLines)
  .map(getHeaderAndBody)
  .reduce(mergeHeadersAndBodies, {})
  
const result = mapToHeadersAndBodies(texts)

console.dir(result)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
0
function myFunction() {
  var str = "The Lion, the Witch and the Wardrobe \n There was once a lion, a \n witch and a wardrobe. The End";
  var res = str.split("\n");
  alert(res);
}

Try using javascript split function.

Farhan Qasim
  • 990
  • 5
  • 18
  • Surely this just gives an alert of both the header and the body, just now as an array? – OliverRadini Apr 05 '19 at 12:40
  • This doesn't answer the question? I'm was under the assumption he had an array of strings under the same format. See my answer for what I mean. – Neil Apr 05 '19 at 12:41
0

Just using the map function of arrays and substring function of strings

let body = ["a \n\ b","c \n\ d","e \n\ f"]
let headers = arrayOfStrings.map(s => s.substring(0,s.indexOf(" \n\ ")))

and linebreak should be just \n without the \ at the end or it will cause problems

wnjstudio
  • 1
  • 1
0

You can use Javascript's split method as others have mentioned.

If the format for all the strings you're parsing is the same, i.e. {header, \n, body} then use the below code. It splits each string into two, then you store each part in separate variables.

const phrases = [
  "The Lion, the Witch and the Wardrobe \n There was once a lion, a witch and a wardrobe. The End",
  "This is the header \n this is the body",
  "This is another header \n this is another body"
]

let headers = []
let bodies = []

phrases.forEach(phrase => {
  let split = phrase.split('\n')
  headers.push(split[0])
  bodies.push(split[1])
})

console.log(headers)
console.log(bodies)
Neil
  • 2,004
  • 3
  • 23
  • 48