1

I use the following code for inserting data to json:

categories: [
                'Tokyo',
                'Jakarta',
                'New York',
                'Seoul',
                'Manila',
                'Mumbai',
                'Sao Paulo',
                'Mexico City',
                'Dehli',
                'Osaka',
                'Cairo',
                'Kolkata',
                'Los Angeles',
                'Shanghai',
                'Moscow',
                'Beijing',
                'Buenos Aires',
                'Guangzhou',
                'Shenzhen',
                'Istanbul'
            ],

It works, but, i want to put this data into an array or string variable like this:

var data = Array('Tokyo',
                'Jakarta',
                'New York',
                'Seoul',
                'Manila',
                'Mumbai',
                'Sao Paulo',
                'Mexico City',
                'Dehli',
                'Osaka',
                'Cairo',
                'Kolkata',
                'Los Angeles',
                'Shanghai',
                'Moscow',
                'Beijing',
                'Buenos Aires',
                'Guangzhou',
                'Shenzhen',
                'Istanbul');
categories: [data],

But, It does not work. Can you help me ? Thanks.

4
  • And categories : data (without the []) does not work? Commented Dec 10, 2012 at 10:02
  • ooops, yes that's right, thanks Commented Dec 10, 2012 at 10:04
  • Is this just that which was your error? Commented Dec 10, 2012 at 10:16
  • this code was worked: categories: [data], Commented Dec 10, 2012 at 10:35

4 Answers 4

1
data: ['Tokyo', 'Jakarta',...] 

is already an array which is part of an object. if you want it as separate variable just create it like this:

var data = ['Tokyo', 'Jakarta',...];

and add it to your object

var yourObject = {categories: data};

you could even do stuff like this:

var yourObject = {categories: new Array('Tokyo', ...)};

but please use the bracket notation

hint: you are creating an "object literal" not a JSON-Object. JSON is just a subset of the object literal. read more here

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

Comments

1

It should be

var data = ['Tokyo','Jakarta','New York','Seoul','Manila','Mumbai','Sao Paulo','Mexico City','Dehli','Osaka','Cairo','Kolkata','Los Angeles','Shanghai','Moscow','Beijing','Buenos Aires','Guangzhou','Shenzhen','Istanbul'];

Comments

0
var categories = [];

var data = ['Tokyo',
            'Jakarta',
            'New York',
            'Seoul',
            'Manila',
            'Mumbai',
            'Sao Paulo',
            'Mexico City',
            'Dehli',
            'Osaka',
            'Cairo',
            'Kolkata',
            'Los Angeles',
            'Shanghai',
            'Moscow',
            'Beijing',
            'Buenos Aires',
            'Guangzhou',
            'Shenzhen',
            'Istanbul'];

for (var i = 0; i < data.length; i++) {
  categories.push(data[i]);
}

1 Comment

@hereandnow78 Excuse me, but the question wasn't that clear either!
0

The answer is that the first part which works is exactly what you described in the second part. So it already works.

Assigning a variable like this:

categories : ['firstElement', 'secondElement'];

makes the json-field categories containing an Array object. You can access 'firstElement' by using categories[0] for example.

is exactly the same like:

var categoties = new Array();
categories.push('firstElement');
categories.push('secondElement');

your second attempt does not work because you can't supply your array values in the constructor new Array(...). If you instantiate it with new Array() you can use .push('value') to add Elements to the array.

1 Comment

I think that the OP just want to have the array initialized in another part of his code and call it in his JSON object...

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.