I've thought that this is how the data will look for every query operation having a unique id (which is 1 & 2 in below example).
var finalData = {
queryData: {
"1": { // here "1" will be the auto-incrementing query operation id generated from somewhere
jiraList: ["abc-123", "bcd-234"],
jiraDetail: [
{
jiraKey: "abc-123",
status: "Closed"
},
{
jiraKey: "bcd-234",
status: "Open"
}
]
},
"2": {
jiraList: ["xyz-987", "wxy-876"],
jiraDetail: [
{
jiraKey: "xyz-987",
status: "Open"
},
{
jiraKey: "wxy-876",
status: "Closed"
}
]
}
}
};
I've a function which accepts the query's unique id & jira List (which is already an array):
var addJiraList = function (key, jiraList) {
// sample params will be key: 1, jiraList: ['abc-123', 'bcd-234']
finalData.queryData[key] = {jiraList: jiraList};
};
So the above function creates a key value pair in finalData.queryData
if not present, and if present, it'll add/update jiraList
with required values to it. This works.
I've another function which accepts the query's unique id & a single jiraDetail which is:
var addJiraData = function (queryKey, extractedData) {
// sample params will be queryKey: 1, extractedData: { jiraKey: "abc-123", status: "Closed" }
finalData.queryData[queryKey]["jiraDetail"].push(extractedData);
};
I want to push the extractedData
coming as parameter in the array finalData.queryData.2.jiraDetail
.
Now the problem is that finalData.queryData.<queryId>
is dynamic & jiraDetail
is an array inside it. So for the first time if queryKey is 1
& extractedData is {something}
, how would I add one item extractedData
into an array (jiraDetail) of queryKey
which is not even created.
At present it is throwing finalData.queryData[queryKey] is undefined
error which is valid as I cannot add inside an object which is not yet created.
Solution in my mind is to check if finalData.queryData[queryKey] is undefined
and if it is undefined then first create the object with empty jiraDetail
array in it so that the following line will not throw undefined error.
finalData.queryData[queryKey]["jiraDetail"].push(extractedData);
There must be something better that can be done which I'm at present unable to think & search. I want to know how would you handle this situation?
Here's the link of the file which will be updated based on the answer that I'll get here.
https://github.com/sunilrebel/spann-jira/blob/master/lib/storageSystem.js
if
part. – Sunil Kumar Dec 30 '14 at 4:01