-6

How do i break down this " 01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|1 " string into 5 integer arrays ?.

For example:

  1. 01|15|59 becomes [01,15,59]

  2. 1|47|6 becomes [1,47,6]

3
  • What did you try? - you just need two splits - and parseInt to not get octal numbers when there is a leading 0 Commented Jul 23, 2017 at 12:59
  • 3
    I always wonder how people end up with such crazy data structures. Why don't you use something standard like JSON? Commented Jul 23, 2017 at 13:00
  • I always wonder how askers completely ignore the suggestions made right under the title of their question. As for the structures - that could easily be completely out of the hands of the asker Commented Jul 23, 2017 at 13:02

1 Answer 1

1
var result = string.trim().split(", ").map(el=>el.split("|").map(n=>+n))

this splits the string into an array of these groups ( yet as string )

  "1|2, 3|4" => ["1|2","3|4"]

then maps this array to a new array containing the strings splitted and converted to numbers:

=> [[1,2],[3,4]]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.I don't really understand what "map(n=>+n))" does though
@briin ["1","2"] => [1,2]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.