maxoya
maxoya

Reputation: 53

Insert data to json from variable

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.

Upvotes: 1

Views: 513

Answers (4)

hereandnow78
hereandnow78

Reputation: 14434

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

Upvotes: 1

Chris
Chris

Reputation: 7855

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.

Upvotes: 0

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

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'];

Upvotes: 1

Taai
Taai

Reputation: 2760

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]);
}

Upvotes: 0

Related Questions