在 JavaScript 中访问和存储数据集

发布于 2024-12-21 21:47:13 字数 984 浏览 1 评论 0原文

我试图找出在 javascript 中存储和访问数据的最佳方法是什么。

我想选择与之前未知的“标识符”相关的数据,或者添加带有新随机“标识符”的新数据集。

我可以用数组或对象来执行此操作吗?

数组存储

{
"data": [
    {
        "key1": "identfifier1",
        "key2": "value2",
        "key3": "value3"
    },
    {
        "key1": "identfifier2",
        "key2": "value2",
        "key3": "value3"
    }
]
}
  • 数组在js中保持有序。这对我来说很重要,因为我想在屏幕上显示其数据。
  • 与“identifier”相关的更复杂的数据集选择(需要某种循环或包含辅助方法,例如 data.containing("identifier").key2)

对象存储

{
"data": {
    "identfifier1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "identfifier2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }
}
}
  • 对象在 js 中不会保持有序,但这对于因为它的数据要显示在屏幕上(我必须在每次输出数据之前对整个对象进行排序),所以可以
  • 轻松快速地选择与“标识符”相关的数据(只需执行 identfifier1.key2 即可例如,不需要循环或辅助方法来搜索包含“标识符”的数据集)

任何想法、方法、最好的方法来做到这一点?一种方式比另一种方式更好?

I am trying to figure out what is the best way to store and access data in javascript.

I want to select the data associated with a "identifier" who´s value is unknown before, or add a new data set with a new random "identifier".

do i do this with an array or object?

array storage

{
"data": [
    {
        "key1": "identfifier1",
        "key2": "value2",
        "key3": "value3"
    },
    {
        "key1": "identfifier2",
        "key2": "value2",
        "key3": "value3"
    }
]
}
  • arrays stay orderd in js. this is important for me becuase i want to display its data on screen.
  • more complicated selection of data set asscoictated with "identifier" (some kind of loop or containing helper method is needed, e.g. data.containing("identifier").key2)

object storage

{
"data": {
    "identfifier1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "identfifier2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }
}
}
  • objects do not stay orderd in js, but that is important for me becuase its data to display on screen ( i would have to sort the whole object before each output of the data )
  • easy and fast selection of data asscoictated with "identifier" (just do identfifier1.key2 for expample, no loop or helper method nedded to search for data set containing "identifier")

any ideas, methods, best ways to do this? one way better than the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-12-28 21:47:13

使用 JSON(第二种方法)。它就是为了这个目的,有许多本机函数和附加库来支持您的工作。可以很容易地使用数据库(例如 CouchDB 或 MongoDB),而无需转换对象。

Use JSON (your second approach). It's meant for that purpose and there are many native functions and additional libraries to support your work. It will be easy to use a database (e.g. CouchDB or MongoDB) with it, without having to convert objects.

迷你仙 2024-12-28 21:47:13

如果读取次数多于写入次数,那么最好使用该对象并保留一个单独的对象键数组,以便在需要时进行排序。

data = {
    identfifier1 : { key1: value1,
            key2: value2,
            key3: value3
          },
    identfifier2 : { key1: value1,
            key2: value2,
            key3: value3
          }
}

当您添加新键时:

data.identifier3 = ...

更新顺序:

var sorted = Object.keys( data ).sort();

以有序的方式迭代:

for( var l = sorted.length, i = 0; i < l; ++i ) {
     alert( data[sorted[i]] );
}

或者您可以仅在绝对需要时进行排序,这样即使写入也会提高性能。

您可以为所有这些创建一个包装“类”,以便快速读取并仅在需要时进行排序。

If you do more reads than writes, then it's better to use the object and keep a separate array of the object keys that can be sorted when needed.

data = {
    identfifier1 : { key1: value1,
            key2: value2,
            key3: value3
          },
    identfifier2 : { key1: value1,
            key2: value2,
            key3: value3
          }
}

And when you add new key:

data.identifier3 = ...

Update the ordering:

var sorted = Object.keys( data ).sort();

To iterate in an ordered way:

for( var l = sorted.length, i = 0; i < l; ++i ) {
     alert( data[sorted[i]] );
}

Or you could just sort only when absolutely needed, this way even writes will be more performant.

You could make a wrapper "class" for all this that is fast for reading and sorts only when needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文