javascript - 是否可以将整数作为键存储在 javascript 对象中?

标签 javascript jquery json types indexing

我在 JSON 中创建一个索引文件,我将其用作我正在处理的 javascript 应用程序的数据库索引。

我的索引看起来像这样:

{
"_id": "acomplex_indices.json",
"indexAB": {
    "title": {
        "Shawshank Redemption": [
            "0"
        ],
        "Godfather": [
            "1"
        ],
        "Godfather 2": [
            "2"
        ],
        "Pulp Fiction": [
            "3"
        ],
        "The Good, The Bad and The Ugly": [
            "4"
        ],
        "12 Angry Men": [
            "5"
        ],
        "The Dark Knight": [
            "6"
        ],
        "Schindlers List": [
            "7"
        ],
        "Lord of the Rings - Return of the King": [
            "8"
        ],
        "Fight Club": [
            "9"
        ],
        "Star Wars Episode V": [
            "10"
        ],
        "Lord Of the Rings - Fellowship of the Ring": [
            "11"
        ],
        "One flew over the Cuckoo's Nest": [
            "12"
        ],
        "Inception": [
            "13"
        ],
        "Godfellas": [
            "14"
        ]
    },
    "year": {
        "1994": [
            "0",
            "3"
        ],
        "1972": [
            "1"
        ],
        "1974": [
            "2"
        ],
        "1966": [
            "4"
        ],
        "1957": [
            "5"
        ],
        "2008": [
            "6"
        ],
        "1993": [
            "7"
        ],
        "2003": [
            "8"
        ],
        "1999": [
            "9"
        ],
        "1980": [
            "10"
        ],
        "2001": [
            "11"
        ],
        "1975": [
            "12"
        ],
        "2010": [
            "13"
        ],
        "1990": [
            "14"
        ]
    }
}
}

所以对于每个 keyword(比如 Pulp Fiction),我都会存储匹配的 document-id(s)

我的问题是整数/数字/非字符串数据,比如上例中的发布年份。它存储为字符串,而我希望它存储为数字。

我正在创建这样的索引条目:

// indices = the current index file
// doc = the document to update the index with
// priv.indices = all indices defined for this application instance
// priv.indices.fields = index fields e.g. "year", "director", "title"
// priv.indices.name = name of this index

priv.updateIndices = function (indices, doc) {
var i, j, index, value, label, key, l = priv.indices.length;

// loop all indices to add document
for (i = 0; i < l; i += 1) {
  index = {};
  index.reference = priv.indices[i];
  index.reference_size = index.reference.fields.length;
  index.current = indices[index.reference.name];

  for (j = 0; j < index.reference_size; j += 1) {
    label = index.reference.fields[j];    // like "year"
    value = doc[label];                   // like 1985

    // if document has a label field (e.g. doc.year = 1985)
    if (value !== undefined) {

      // check if the index file already contains an entry for 1985
      index.current_size = priv.getObjectSize(index.current[label]);

      if (index.current_size > 0) {
        // check if the document id is already in the index
        // in case the data is updated (e.g. change 1982 to 1985)
        key = priv.searchIndexByValue(
          index.current[label],
          doc._id,
          "key"
        );
        if (!!key) {
          delete index.current[label][key];
        }
      }
      // create a new array if 1985 is not in the index yet
      if (index.current[label][value] === undefined) {
        index.current[label][value] = [];
      }
      // add the document id to an existing entry
      index.current[label][value].push(doc._id);
    }
  }
}
return indices;

};

这工作正常,除了我想存储为非字符串(整数、数字或日期时间)的字段,如上例中的年份最终在我的索引中作为字符串。

问题:
是否可以在 JSON 文档中存储“非字符串”类型?如果是这样,我是否也可以将键/值对的键存储为“非字符串”元素。

如果不是,我是否必须在我的索引定义中添加一个参数来声明每个键的类型,以便在我遇到它时修改键字符串,或者有更好的方法吗?

谢谢!

最佳答案

Is it at all possible to store "non-string" types in a JSON document?

是的。属性的值可以是字符串、数字、 bool 值、对象、数组或 null(undefined 是一个值得注意的异常(exception) - 它是原生 JavaScript 类型,但不是有效的 JSON 值)。

Can I also store the key of a key/value pair as a "non-string" element?

没有。键名必须始终是字符串。但是,这并不意味着您不能将该字符串解析为其他一些 JavaScript 类型。例如,如果您有一个字符串但需要一个数字,您可以使用 parseInt 函数或一元 + 运算符。

参见 JSON grammar了解更多详情。

关于javascript - 是否可以将整数作为键存储在 javascript 对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687474/

相关文章:

jquery - 无法覆盖外部文件的溢出 CSS 属性

jquery - 从 div 中删除元素

json - DocumentDB,对 JSON 文档使用小字段名称以最小化长度

javascript - Node js远程JSON对象返回未定义

javascript - 如何循环遍历并将值对添加到 JSON 对象?

javascript - 根据值从对象中删除项目

javascript - CoffeeScript - 数组过滤不起作用

javascript - 主干 Marionette 复合 View 渲染模板

javascript - 生成并下载网页截图而不丢失样式

javascript - 如何在processing.js中访问json数据