# Scala: Condense Array [(String, Array [Double])] to Array [(String)]

I have an array that looks like this array ((1, Array (1.0,0.0,3.0)), (2, Array (0.0,2.0,1.0))) that I want to include, and an array that looks like: Array ((1,1,0,0,0,3,0), (2,0,0,2,0,1,0)).

Is there an easy way to do this? I'm guessing there is some kind of map I can make, but I haven't been able to figure out the syntax.

Thank.

+3

source to share

2 answers

Specify the types of input and output. As I understand the task, it is Array [String, Array [Double]] => Array [Array [Double]]

```
scala> val r = Array(("1", Array(1.0, 2.0, 1.0, 0.0, 3.0)), ("2", Array(0.0, 2.0, 1.0)))
r: Array[(String, Array[Double])] = Array((1,Array(1.0, 2.0, 1.0, 0.0, 3.0)), (2,Array(0.0, 2.0, 1.0)))
scala> val res = r map { case (s, Array(xs @ _*)) => (s.toDouble +: xs).toArray }
res: Array[Array[Double]] = Array(Array(1.0, 1.0, 2.0, 1.0, 0.0, 3.0), Array(2.0, 0.0, 2.0, 1.0))
```

+3

source to share

You can do it:

```
a.map { case (a, Array(b,c,d)) => (a,b,c,d) }
```

```
scala> val a = Array((1,Array(1.0,0.0,3.0)), (2,Array(0.0,2.0,1.0)))
a: Array[(Int, Array[Double])] = Array((1,Array(1.0, 0.0, 3.0)), (2,Array(0.0, 2.0, 1.0)))
scala> a.map({ case (a, Array(b,c,d)) => (a,b,c,d) })
res4: Array[(Int, Double, Double, Double)] = Array((1,1.0,0.0,3.0), (2,0.0,2.0,1.0))
```

Solution with support for up to 22 tuples. Of course, even this assumes that all members of the array are of the same length.

```
a.map {
case (a, Array(b)) => (a,b)
case (a, Array(b,c)) => (a,b,c)
case (a, Array(b,c,d)) => (a,b,c,d)
// pseudo-scala
case (n1, Array(n2,...,n22)) => (n1,n2,...,n22)
}
```

+4

source to share