【问题标题】:Is it possible to store integers as keys in a javascript object?是否可以将整数作为键存储在 javascript 对象中?
【发布时间】:2013-01-19 04:42:05
【问题描述】:

我正在 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(如低俗小说),我都会存储匹配的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 文档中存储“非字符串”类型?如果是这样,我是否也可以将键/值对的键存储为“非字符串”元素。

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

谢谢!

【问题讨论】:

  • 根据json spec,您可以在任何可以拥有value 的地方拥有一个号码。所以对象的键必须是字符串,但值可以是数字。此外,数组中的任何值都可以是数字。

标签: javascript jquery json types indexing


【解决方案1】:

是否可以在 JSON 文档中存储“非字符串”类型?

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

我还可以将键/值对的键存储为“非字符串”元素吗?

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

更多详情请参阅JSON grammar

【讨论】:

    【解决方案2】:

    根据json spec,您可以在任何可以有值的地方有一个数字。所以对象的键必须是字符串,但值可以是数字。此外,数组中的任何值都可以是数字。

    不过,规范并不重要;我认为问题在于这一行:

    index.current[label][value].push(doc._id);
    

    当您阅读doc._id 时,这是一个字符串。如果要将其作为数字存储在 JSON 中,则需要对其进行强制转换:

    index.current[label][value].push(parseInt(doc._id, 10));
    

    还要注意just numbers as IDs is not valid HTML

    【讨论】:

    • 好主意。但问题是value 是一个字符串还是一个数字。
    【解决方案3】:

    不,你不能,在 JSON 中键是字符串。

    你能做的最好的就是存储这些键的字符串表示,无论是整数还是对象(更复杂,你必须构建一个序列化函数)。

    如果你只想使用从 0 开始的连续整数键,那么你可以使用数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2022-07-21
      • 2020-12-27
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多