मैं जेएसओएन में एक इंडेक्स फ़ाइल बना रहा हूं, जिसे मैं एक जावास्क्रिप्ट एप्लिकेशन के लिए सॉर्ट-ऑफ-डेटाबेस इंडेक्स के रूप में उपयोग कर रहा हूं जिस पर मैं काम कर रहा हूं।क्या जावास्क्रिप्ट ऑब्जेक्ट में पूर्णांक को चाबियाँ के रूप में स्टोर करना संभव है?
मेरे सूचकांक इस तरह दिखेगा:
{
"_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 दस्तावेज़ में संभव है? यदि हां, तो क्या मैं एक कुंजी/मान जोड़ी की कुंजी को "गैर-स्ट्रिंग" तत्व के रूप में भी संग्रहीत कर सकता हूं।
यदि नहीं, तो क्या मुझे अपनी अनुक्रमणिका परिभाषाओं में पैरामीटर जोड़ना होगा ताकि प्रत्येक कुंजी के प्रकार को घोषित करने के लिए कुंजी-स्ट्रिंग को संशोधित करने के लिए या इसे करने का बेहतर तरीका हो?
धन्यवाद!
[जेसन स्पेक] (http://json.org/) के मुताबिक, आपके पास एक नंबर हो सकता है जहां आपके पास 'value' हो। तो किसी ऑब्जेक्ट की कुंजी * एक स्ट्रिंग होनी चाहिए, लेकिन मान एक संख्या हो सकती है। इसके अलावा किसी सरणी में कोई भी मान एक संख्या हो सकता है। – jbabey