Sto creando un file indice in JSON, che sto usando come indice del database per un'applicazione javascript su cui sto lavorando.È possibile memorizzare numeri interi come chiavi in un oggetto javascript?
mio indice sarà simile a questa:
{
"_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"
]
}
}
}
Quindi per ogni keyword
(come Pulp Fiction), che sto memorizzare il corrispondente document-id(s)
.
Il mio problema è con numeri interi/numeri/non stringa, come l'anno di rilascio nell'esempio precedente. Questo è memorizzato come una stringa, mentre speravo che sarebbe stato memorizzato come un numero.
Sto creando le voci di indice in questo modo:
// 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;
};
Questo funziona, eccetto che i campi che voglio memorizzare come non stringhe (interi, numeri o datetime), come l'anno nell'esempio sopra, finiscono come stringhe nel mio indice.
Domanda:
È a tutto il possibile memorizzare tipi "non-string" in un documento JSON? In tal caso, posso anche memorizzare la chiave di una coppia chiave/valore come elemento "non stringa".
In caso contrario, dovrei aggiungere un parametro alle mie definizioni di indice che dichiarano il tipo di ciascuna chiave per modificare la stringa di chiavi quando ci si imbatte o esiste un modo migliore per farlo?
Grazie!
secondo [json spec] (http://json.org/), puoi avere un numero ovunque tu possa avere un valore. Quindi la chiave di un oggetto * deve * essere una stringa, ma il valore può essere un numero. Inoltre, qualsiasi valore di un array può essere un numero. – jbabey