0

I am trying to create a key-value pair javascript object as follows:

var p={'revEv':[{'EventsId':rand, 
        if(Ext.getCmp('Val_'+id).getValue()!='E')
        'Status':Ext.getCmp('Val_'+id).getValue(),
        'core':Ext.fly('coreVal_'+id).dom.innerHTML,
        'transactionType':{'transactionTypeId':Ext.fly('oppVal_'+id).dom.innerHTML,'description':Ext.fly("oppDesc_"+id).dom.innerHTML},
        'timing':Ext.getCmp('quarter_'+id).getValue()},]}

As seen above, for a particular entry 'Status' - I need to create/insert the key only if a particular condition is satisfied.
What is an elegant way to do this in javascript?

1
  • That's not valid JavaScript. You can't use if statement in object literals. Commented Aug 12, 2014 at 10:44

2 Answers 2

1

Initialize everything else and then set status in an if statement:

var p={'revEv':[{'EventsId':rand, 
    'core':Ext.fly('coreVal_'+id).dom.innerHTML,
    'transactionType':{'transactionTypeId':Ext.fly('oppVal_'+id).dom.innerHTML,'description':Ext.fly("oppDesc_"+id).dom.innerHTML},
    'timing':Ext.getCmp('quarter_'+id).getValue()},]}

if (Ext.getCmp('Val_'+id).getValue()!='E') {
    p.revEv[0].Status = Ext.getCmp('Val_'+id).getValue(),
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can just do

var p = {};
if(condition) p.key = value;

Comments

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.