0

I have an array of strings in JavaScript and I want to split them into "paragraphs" by grouping them together based on blank lines. for instance:

["a", "b", "", "c", "", "d", "e", "f"] => [["a", "b"], ["c"], ["d", "e", "f"]]

It's exactly like string.split() but with an array as the input instead of a string.

Of course, I could write this function myself using a loop or maybe reduce but it sounds like quite a generic function. I'm wondering if there is anything in lodash or similar that could do it.

7
  • 1
    A quick search brought (possible) solutions for this topic here on SO... -> How much research effort is expected of Stack Overflow users? Commented Apr 15, 2021 at 7:32
  • 3
    What have you tried so far to solve this on your own? Commented Apr 15, 2021 at 7:32
  • 2
    @Ergis SO is not a free "I need XYZ, find a solution for me" service Commented Apr 15, 2021 at 7:39
  • 1
    @Andreas He is literally just asking if there is something in lodash for it. If someone has that information, he could share. Otherwise, this could end-up as an issue for a 'feature' in lodash's github. So why do you have totake it so hard? Commented Apr 15, 2021 at 7:41
  • 1
    @Andreas I searched lodash, stackoverflow, underscore and npmjs. most of the solutions were splitting into fixed size chunks but I couldn't find anything about splitting based on some condition to detect a delimiter. Of course I could write it myself and not share it with anyone but if we all did that then we wouldn't have useful libraries like lodash and resources like SO Commented Apr 15, 2021 at 8:13

3 Answers 3

2

you mean like this ?

let data = ["a", "b", "", "c", "", "d", "e", "f"]
let temp = []
let result = []
data.map(x => {  
  if(x) {
    temp.push(x)
  } else {
    result.push(temp)
    temp = []
  }
})

temp.length != 0 ? result.push(temp) : '' 
console.log(result)

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

2 Comments

Of course I could write this function myself using a loop or maybe reduce but it sounds like quite a generic function. I'm wondering if there is anything in lodash or similar that could do it. How does your answer relate to what is being asked?
Not generic enough.
0

I'm not sure lodash can resolve it at this time.

In the meanwhile, you can do the trick in this way

const array = ["a", "b", "", "c", "", "d", "e", "f"];
const delimiter = ",";
var index = 0;

const result = array.reduce((acc, curr) => {
  if(curr == "") index++;
  else acc[index] = (acc[index] || "") + delimiter + curr;
  
  return acc;
}, [])
console.log(result.map(r => r.substring(1).split(delimiter)));

8 Comments

Not my DV but if you're not going to post an answer with lodash then vote to close as one of the duplicates you can find here on SO.
IMO, this question is quite useful (Except the IO didn't provide effort/what tried so far). Hopefully, lodash will have the solution soon in this case. So that is the reason why I still wanna answer @Andreas
This is not a generic solution. It doesn't even work for strings - try with ["a", b", "", "c,d",] and you get the second group as ["c", "d"]. Adjusting the delimiter doesn't help much as it might still clash with the data. Further problem is if the data is not a string: [1, 2, "", 3, 4] will have the data types changed by this. having objects will just fail even worse. Ultimately splitting is fundamentally flawed approach and not suitable as a generic solution.
"IMO, this question is quite useful..." - That might be the case, but this doesn't change the fact that there are already suitable other questions/answers hence this should be closed as duplicate.
@Andreas please report the question as a duplicate and link to one or more of the solutions you already found. the only solution I found was for swift, not JS
|
-1

If it's a simple formula that doesn't use loops, you can create it like this:

arr.map(v => v || ",").join("").split(",").map(v => v.split(""))

If you plan on getting paragraphs values a lot, you might want to define a function for it.

First, put this in your code somewhere:

Array.prototype.paragraphs = function(){
  return this.map(v => v || ",").join("").split(",").map(v => v.split(""))
}

Now:

["a", "b", "", "c", "", "d", "e", "f"].paragraphs()

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.