-1

How can I split an object like ["text1 text2"] to only show the text1 text2 string in javascript?

3
  • 3
    ["text1 text2"] is an Array, not a String. It doesn't have a split() method Commented Oct 1, 2016 at 4:50
  • simple use your arrayname[0]. It will return text1 text2 Commented Oct 1, 2016 at 6:57
  • Possible duplicate of Split JavaScript string with conditions Commented Oct 1, 2016 at 8:29

2 Answers 2

2

You just want to access the value inside? ["text1 text2"][0] would return the string, text1 text2

Sign up to request clarification or add additional context in comments.

Comments

0

As Tibrogargan said in the comments above, ["text1 text2"] is an array. This array contains one string value "text1 text2" which can be accessed either by using it's index inside an array (to get that specific item) or by looping through all the items inside the array (if you want all items).

For your case, it's easy to use index.

var stringArray = ["text1 text2"];
console.log(stringArray[0]);
// text1 text2 should be logged into console

Check here for more documentation.

Comments

Your Answer

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.