3

Let's say I have a single element array ["someStringHere"] (I don't know in advance what the string will be) and I want to access the string so that my function returns the string and not the array, how could I do so without using an indexer (ex: array[0])?

In other words, it would be as if I could delete the brackets so that I just had the string.

asked Jan 16, 2016 at 20:49
8
  • 1
    Array.prototype.pop() Commented Jan 16, 2016 at 20:51
  • @Andreas - pop() would remove the element and I'm not sure if the OP wants it removed the array. Commented Jan 16, 2016 at 20:52
  • 2
    @AlanH - the question is simple, but what you're looking for is unusual, would you mind elaborate on why? Commented Jan 16, 2016 at 20:54
  • You cant. Arrays are string and indexes, this is all. So you can only access via index or string. The only useful thing using the string is to check if the string is contained in the array. Commented Jan 16, 2016 at 20:54
  • @Will working through some exercises, and not being able to use an indexer is one of the conditions. Commented Jan 16, 2016 at 20:54

2 Answers 2

5

Every object has a toString method. You could go about using this method, though it's confusing why you'd want to. Another option is the join method.

var someArr = ['some string here'];
console.log(someArr.toString());
console.log(someArr.join());

According to MDN:

The Array object overrides the toString method of Object. For Array objects, the toString method joins the array and returns one string containing each array element separated by commas.

See here for specifics.

answered Jan 16, 2016 at 20:56
Sign up to request clarification or add additional context in comments.

3 Comments

Hm. didn't know it applied to arrays. It's a wacky solution, but it works. Still, I was hoping for something different.
@AlanH What kind of different? I've updated my answer with the join method also. There aren't very many other ways
"There aren't very many other ways" => there's (at least) one more, and it's the quirkiest.
1

Amusingly, that works just by forcing it to a string type:

var someArr = ['some string here'];
console.log( "" + someArr );

And one more for the road:

someArr.forEach (function (s) { console.log(s) });
answered Jan 16, 2016 at 21:08

1 Comment

which actually is the same as using the toString method: "JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation" (MDN). Nonetheless, still an alternative way.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.