0

I have the following javascript object:

result = {
    "banking6dig":{
        "GM-B-001":{
            "releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"
        },
        "GM-B-002":{
            "releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"
        }
    },
    "paynpark":[]
} 

Explanation:

"banking6dig" and "paynpark" are applications
"banking6dig" has two subversions : "GM-B-001" and "GM-B-002"
"paynpark" has no subversions.

Additionally, each subversion has its own properties,viz, "releaseDate", "noOfHex", and "versInfo".

This object "result" is built after a php request, so it can have any number of apps and subversions; the format, however is ALWAYS the same.

I have tried this on jsFiddle: http://jsfiddle.net/2JLtZ/1/

  1. How do I find out the number of "subversions" in each "app"? (I get some 40 "subversions"!)
  2. How do I add an app to the object "result", e.g., "electricity" with its properties reset?
  3. How do I add a subversion to "paynpark", e.g. "fixedRate"?
  4. How do I modify a subversions properties, for example change "releaseDate" of "GM-B-001" to "3/12/2012 14:46"?

2 Answers 2

1

1.

var i=0;
for (var j in result['banking6dig']) i++;
alert('banking6dig has '+i+' subversions'):

2.

result.electricity={}

3.

paynpark.fixedRate={};

Thanks AlienWebguy for pointing this out.

4.

result['banking6dig']["GM-B-001"]["releaseDate"]="3/12/2012 14:46";
Sign up to request clarification or add additional context in comments.

2 Comments

Arrays are objects in JS. You can add any property you want. var foo = []; foo.foo = 'bar'; alert(foo.foo);. All it means is foo's prototype is Array instead of Object.
@AlienWebguy I stand corrected, didn't know that and just verified it. Thanks, editing my answer.
1
result = {"banking6dig":{"GM-B-001":{"releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"},"GM-B-002":{"releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"}},"paynpark":[]};

var app = [],
    svn = {};

for (var _app in result) {
    if(result.hasOwnProperty(_app)){
        app.push(_app);
        svn[_app] = [];
        for (var _svn in result[_app]){
            if(result[_app].hasOwnProperty(_svn)){
                svn[_app].push(_svn);
            } 
        }
    }  
}

// How man apps?
alert(app.length);

// How many subversions?
alert(svn['banking6dig'].length);

// Add an app 'electricity'
result.electricity = {};

// Add subversion to paynpark
result.paynpark.fixedRate = {}

// Add electricity
result.electricity = {};
app.push('electricity');

// Modify subversion
result.banking6dig['GM-B-001'].releaseDate = '3/12/2012';

console.log(result);

Demo: http://jsfiddle.net/AlienWebguy/DpCTP/

1 Comment

wowwwwwwwww.... ill be using that, thanks so much, pls do reply if I do have further doubts...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.