Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
1  
You should be checking for the keys existence either way - it's not a big deal... not sure why that's a problem? – tymeJV Dec 29 '14 at 20:37
    
It's not a problem actually, just confirming that if the function is called thousand times, checking every time existence is a big deal or not. Because it'll actually create space once & rest time it'll skip the if part. – Sunil Kumar Dec 30 '14 at 4:01

1 Answer 1

up vote 1 down vote accepted

You indeed need to check if the key is present in finalData.queryData and if not create. Look at this solution:

var addJiraData = function (queryKey, extractedData) {
    if (!finalData.queryData[queryKey] || !finalData.queryData[queryKey]["jiraDetail"])
    {
        //one of the objects isn't present, let's create it.
        !finalData.queryData[queryKey] ? addJiraList(queryKey, []) : null; //add missing queryKey and empty array as jiraList.
        finalData.queryData[queryKey]["jiraDetail"] = []; //create empty jiraDetail array.
    }
    //then fill it with data, since it is available now.
    finalData.queryData[queryKey]["jiraDetail"].push(extractedData);
};
share|improve this answer
    
Hey Thanks. That's exactly what I thought. Seems checking every time is not a big deal even if the function is called thousand of times. – Sunil Kumar Dec 30 '14 at 4:02
    
Data consistency is in this case more important. The function isn't too heavy, so that should not effect performance. – Mouser Dec 30 '14 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.