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 java开发者_开发技巧script 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. :)
精彩评论