简体   繁体   中英

How to put data from a file in to javascript array

I give user an option to choose a dataset. Currently I have a dataset with 14 columns and I give an option to user to choose two dimensions out of it .

How do I load this data selected by user in to a javascript array so that my javascript apps can read it ???

any suggestions ??

thanks :)

Without knowing the specifics of your implementation, I can only answer with very vague generalities. I will first assume that you are not actually trying to get data from a "file" to javascript, but that you mean from a "script" to javascript.

I will also assume that you already have the data you're after in some kind of an array. Since you did not specify the server-side language, I will assume PHP:

// sample data array
$the_data = array(
 'color'=>'red',
 'type'=>'apple'
);
print json_encode($the_data);

The preceding code illustrates conversion from a server-side data type to a string which can be parsed and thus converted to a client-side object. On the client side, you will need to use some kind of method to convert this json encoded string to an object. Using the vanilla JSON object:

JSON.decode(responseText);

Your question leaves many details to assume. If you clarify your question, I can provide a much more specific answer. Otherwise/until then, I hope this helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM