1

I have the following array:

arr = [["Example"]]

I need to reduce it to just "Example" (basically, just remove the array).

I know I could do arr[0][0], but am curious if there's a simple method to just remove the string from the array without using indexes.

For clarification...there will only ever be a single item in the array.

2 Answers 2

7

For a single item, you can use:

[['array']].join
=> 'array'

Updated with more examples

If you have multiple items, the strings will be combined:

[['array'], ['array']].join
=> 'arrayarray'

And if you pass a parameter to the join method:

[['array'], ['array']].join('&')
=> 'array&array'
Sign up to request clarification or add additional context in comments.

5 Comments

OP question is just about one element.
Could be, but it could be just an example. I'm going to make an edit to clarify your answer.
@screenmutt it works for multiple items : [["123"],["456"]].join returns "123456"
@MarcinDoliwa Only if you want to combine the object, that wasn't specified in the question.
If you dont want, just pass separator [["123"],["456"]].join("|") outputs "123|456"
2

While this is not as efficient as [0][0], it will still work:

arr.flatten.first

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.