ANucool Mittal ANucool Mittal - 5 months ago 12
Ajax Question

how to iterate json object array?

i have a json array like

[{ "qbCondition": "OR"},{"fieldId": "ca.Primary_Diagnosis_Dt" ,"operator":"not_equal","fieldValue": "1991/11/17"},{"fieldId": "ca.Clinical_Stage","operator": "not_equal","fieldValue": "I"}]


I just want to put this array into

var rules_widgets = { condition: 'qbCondition', rules: [{ id: 'fieldId',operator: 'operator', value: 'fieldValue'},{id: 'fieldValue',operator: 'operator', value: 'fieldValue' },]};


dynamically i tried this

$(data).each(function(idx, obj){
$(obj).each(function(key, value){
console.log(JSON.stringify(key) + ": " + JSON.stringify(value));
});
});


but don't know how to put this values dynamically to create above json please help

Answer

You can do something like this

var x = [{  "qbCondition": "OR"},{"fieldId": "ca.Primary_Diagnosis_Dt" ,"operator":"not_equal","fieldValue": "1991/11/17"},{"fieldId": "ca.Clinical_Stage","operator": "not_equal","fieldValue": "I"}]

var rules = [];
for(var i=1; i<x.length; i++) {
    rules.push({
    id: x[i].fieldId,
    operator: x[i].operator,
    value: x[i].fieldValue
  })
}

var rules_widgets = {
    condition: x[0].qbCondition,
  rules: rules
}
Comments