0

I am pretty new to java and javascript and I have a problem.

I have an array in the form of a string which looks like below in javascript.

[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]

In this, I need to get the first array [0,'Apple','Banana'] and save it as a separate array and then I need to get the second array ['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree'] and display it as a text in text area like this.

The Apple fruit, comes from Apple Tree
The Banana fruit, comes from Banana Tree

How can I achieve this in javascript?

Please need some help as I have googled about this problem intensively.

Thanks in advance.

4
  • eval this string so it will be converted to array. Then you can easily access. Commented Jul 19, 2013 at 7:43
  • No - don't eval it - it's seriously bad practice and it's not necessary. Commented Jul 19, 2013 at 7:47
  • @ShuklaJay - Depending on where the string value comes from, using eval can be very unsafe. Much better to use JSON.parse(string) to turn this into an array. Commented Jul 19, 2013 at 7:47
  • BTW, the plant that bananas grow on is not a tree. Commented Jul 19, 2013 at 7:48

4 Answers 4

3

You can use var arr = JSON.parse(str) to convert your string to a Javascript array. Then access the members of the array nomrally, eg. arr[0]

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

Comments

1

You can do something like this if you have a string.

var arr = JSON.parse("[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]");

var seprateArr = arr[0];

var text = arr[1].join(" ");

2 Comments

I tried this but it seems JSON doesn't work. My javascript code stops on the line var arr = JSON.parse("[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]");
YOu can use eval("("[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]")") instead of JSON.parse, but I would not recommend it if your data is not from a trusted source.
1

"looks like below in javascript" means you have an array of strings to me.

  1. in the case you have this

    var myarray = [
               [0,'Apple','Banana'],
               ['The Apple fruit, comes from Apple Tree', 
                'The Banana fruit, comes from Banana Tree']
              ];
    

    just assign the array to a variable

    var first = myarray[0];
    

    for the second problem, use .join() method of arrays

    var second = myarray[1].join(' ');
    
  2. or in case you have

    var mystring = "[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]";
    

    then you'll need to convert to array first.

    var myarray = JSON.parse(mystring);
    

1 Comment

OP said that the data was in the form of a string.
0

Try this

var a =eval("[[0,'Apple','Banana'],['The Apple fruit, comes from Apple Tree', 'The Banana fruit, comes from Banana Tree']]");
var b = a[0];
var c = a[1];

now b and c are your new arrays;

you can also use the above method mentioned by mohkhan but it is not a valid json; if you have a valid JSON string then above one will be the preferred methos

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.