Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将整数作为键存储在javascript对象中?_Javascript_Jquery_Json_Types_Indexing - Fatal编程技术网

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

是否可以将整数作为键存储在javascript对象中?,javascript,jquery,json,types,indexing,Javascript,Jquery,Json,Types,Indexing,我正在用JSON创建一个索引文件,我正在使用它作为javascript应用程序的一种数据库索引 我的索引将如下所示: { "_id": "acomplex_indices.json", "indexAB": { "title": { "Shawshank Redemption": [ "0" ], "Godfather": [ "1" ], "Godfather

我正在用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"
        ]
    }
}
}
// 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;
因此,对于每个
关键字
(如低俗小说),我将存储匹配的
文档id

我的问题是整数/数字/非字符串数据,就像上面例子中的发布年份一样。这是存储为字符串,而我原本希望它会存储为数字

我正在创建如下索引项:

{
"_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"
        ]
    }
}
}
// 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;
//索引=当前索引文件
//doc=用于更新索引的文档
//priv.index=为此应用程序实例定义的所有索引
//priv.indexes.fields=索引字段,例如“年”、“主管”、“职务”
//priv.index.name=此索引的名称
priv.updateIndices=函数(索引,文档){
变量i,j,索引,值,标签,键,l=priv.indexs.length;
//循环所有索引以添加文档
对于(i=0;i0){
//检查文档id是否已在索引中
//如果数据更新(例如,从1982年更改为1985年)
key=priv.searchIndexByValue(
index.current[标签],
文件编号:,
“钥匙”
);
如果(!!键){
删除索引。当前[标签][键];
}
}
//如果索引中还没有1985,请创建一个新数组
if(index.current[label][value]==未定义){
索引。当前[标签][值]=[];
}
//将文档id添加到现有条目中
index.current[label][value].push(doc.\u id);
}
}
}
回报指数;
})

这很好,除了我想存储为非字符串(整数、数字或日期时间)的字段(如上面示例中的年份)在我的索引中以字符串结束之外

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

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

谢谢

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

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

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

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


有关更多详细信息,请参阅。

不可以,在JSON中,键是字符串

最好是存储这些键的字符串表示形式,无论是整数还是对象(更复杂的是,必须构建序列化函数)

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

根据,可以在任何可能有值的地方使用数字。因此,对象的键必须是字符串,但值可以是数字。此外,数组中的任何值都可以是数字

不过,规格与重点无关;我认为问题在于这一行:

index.current[label][value].push(doc._id);
当您阅读
doc.\u id
时,这是一个字符串。如果要将其作为数字存储在JSON中,则需要强制转换:

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

还要注意的是,根据

,您可以在任何有
值的地方拥有一个数字。因此,对象的键必须是字符串,但值可以是数字。数组中的任何值都可以是数字。好主意。但问题是
value
是字符串还是数字。