简体   繁体   English

用JavaScript存储以下内容的最佳方法是什么?

[英]What is the best way to store the following in Javascript?

I need to store country, state, city, and county info in Javascript. 我需要用Javascript存储国家,州,城市和县的信息。 Example: 例:

   Country    State           City           County 
    USA        CA           Oakland         Alameda 
    USA        CA           Markleeville    Alpine 
    USA        WA           Seattle         king
    ...
   CANADA     Ontario       Toronto          -
    ...

    UK    Grater London     London           -        

What is the best storage or data structure to store such information in Javascript? 在Java中存储此类信息的最佳存储或数据结构是什么? I was thinking of array but there must be other easier way. 我当时在考虑数组,但肯定还有其他更简单的方法。

Thanks 谢谢

How much data are we talking about? 我们在谈论多少数据?

The most straight-forward way would be an array (of objects), which may not scale to tens of thousands of records: 最简单的方法是(对象)数组,它可能无法扩展到成千上万条记录:

var data = [
   { country: 'USA', state: 'CA', city : 'Oakland', county: 'Alameda' } 
   ....
];

If you want the easiest way to process you should store it in JSON . 如果您想要最简单的处理方式,则应将其存储在JSON中

[
   { country: 'USA', state: 'CA', city : 'Oakland', county: 'Alameda' },
   { country: 'USA', state: 'CA', city : 'Markleeville', county: 'Alpine' },
   { country: 'USA', state: 'CA', city : 'Seattle', county: 'king' }
   ....
];

If you want the smallest filesize possible you should use CSV : 如果您想要最小的文件大小 ,则应使用CSV

Country,State,City,County 
USA,CA,Oakland,Alameda 
USA,CA,Markleeville,Alpine 
USA,WA,Seattle,king

If you want something in between you can use compressed JSON format : 如果您想在两者之间使用,可以使用压缩的JSON格式

[

  "cols": ["country", "state", "city", "county"],
  "rows": [["USA", "CA", "Oakland", "Alameda"],
           ["USA", "CA", "Markleeville", "Alpine"],
           ["USA", "CA", "Seattle", "king"],
          ... ]
};

Explanation 说明

With JSON you get a native Javascript object which is loaded via <script> tag or Ajax. 使用JSON,您将获得一个本机Javascript对象,该对象可通过<script>标记或Ajax加载。 If you go with the compressed JSON format you still get a native object, the only difference is in how you use the data. 如果使用压缩的JSON格式,您仍然会得到一个本机对象,唯一的区别在于使用数据的方式。 With CSV you should write your own parser, but the small size may worth it. 使用CSV,您应该编写自己的解析器,但较小的尺寸可能值得。

You can read about the performance implications of JSON and a custom format here: 您可以在此处了解JSON和自定义格式对性能的影响:

Building Fast Client-side Searches for Flickr 为Flickr建立快速的客户端搜索

Almost like Thilo wrote, but I would have store them in a tree structure. 几乎就像Thilo所写的一样,但是我会将它们存储在树形结构中。 Each tree one country/state and each tree in a different file : 每棵树一个国家/州,每棵树在不同的文件中:

    var data = { country: 'USA', states: [{name: 'CA', cities: [{'name':'Oakland', county: 'Alameda' }]
}] 
}

Each of those in different file, and when loaded they should overwrite each other (ie all of the files start with var data=... 每个文件都在不同的文件中,并且在加载时它们应该相互覆盖(即,所有文件都以var data=...开头var data=...

If you store that in DB/server side code, make sure those files are being cached, no problem in caching megas of data. 如果将其存储在数据库/服务器端代码中,请确保正在缓存那些文件,在缓存大量数据时没有问题。

Since the requirement seems to be javascript regardless of the scenario, then use JSON. 由于无论情况如何,要求似乎都是javascript,因此请使用JSON。 If you don't want to store everything on the client side, think about calling a web service that would return only the data you need is JSON format. 如果您不想将所有内容存储在客户端,请考虑调用仅返回所需数据为JSON格式的Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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