-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]

briin
  • 24
  • 1
  • 5
  • What did you try? - you just need two splits - and parseInt to not get octal numbers when there is a leading 0 – mplungjan Jul 23 '17 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? – str Jul 23 '17 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 – mplungjan Jul 23 '17 at 13:02

1 Answers1

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]]
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151