Reputation: 3337
I have a simple nodejs get that runs like this:
app.get('/customer/:ent_cust_id', function (req, res, next) {
var query = 'Select * from entcustinfo where ent_cust_id = ' + req.params.ent_cust_id;
console.log('Select * from entcustinfo where ent_cust_id =' + req.params.ent_cust_id);
client.execute(query, function (err, result) {
if (err) return next (err);
var row = result.rows[0];
if (result.rows.length==0) {
res.send('ent_cust_id: ' + req.params.ent_cust_id + ' not found. Not all data is loaded.');
} else {
var row = result.rows[0];
//Response
res.json({ent_cust_id: req.params.ent_cust_id, accts: row.get('accts'), offers: row.get('offers')});
};
});
});
and will return a result into an array like this:
{"ent_cust_id":"1013686356",
"accts":{
"3017":{"popn_typ_cd":"CNSMR_DIRCT_CHKG","prod_type_cd":"DDA","ent_prod_cd":"CNSDD","curr_amt":"14.67","bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0},
"2752":{"popn_typ_cd":"CNSMR_LENDG","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":1},
"888":{"popn_typ_cd":"CNSMR_DIRCT_MORT","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0}
},
"offers":{
"5998":{"contacted":"Y","hit_home_date":"2015-06-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
"6998":{"contacted":"Y","hit_home_date":"2015-07-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
"7998":{"contacted":"Y","hit_home_date":"2015-08-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"},
"506A":{"contacted":"Y","hit_home_date":"2015-06-18T04:00:00.000Z","creative":"Availability CIT","channel":"EM","campaign":"FUNDS_AVAILABILITY_CIT","campaign_tp":"NOTIFICATION"},
"06_A":{"contacted":"Y","hit_home_date":"2015-06-29T04:00:00.000Z","creative":"Mob5X","channel":"EM","campaign":"EMAIL","campaign_tp":"UPSELL"},
"5009":{"contacted":"Y","hit_home_date":"2015-06-05T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
"6009":{"contacted":"Y","hit_home_date":"2015-07-09T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
"7996":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"},
"R_33S":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"},
"R_34S":{"contacted":"Y","hit_home_date":"2025-12-31T05:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"},
"R_22":{"contacted":"Y","hit_home_date":"2015-06-22T04:00:00.000Z","creative":"1Offer","channel":"EM","campaign":"TIERED CASH","campaign_tp":"SELL"}
}
}
I see how I can get the first level of the array, but what if I want to get any other part of the array.
What if I also want to check if in offers
, the contacted
field is marked as Y
at all in any of the results in teh offers part?
How do I do that?
EDIT:
seems like this is just a string and not an array. So maybe the question then is, how do I transform this into an array and/or json object?
Upvotes: 0
Views: 258
Reputation: 1329
First-off, I don't see any arrays, so I'm a little confused about the first question?
But in answer to your second question, if your data has been evaluated into an object called 'data', which is an array of the above JSON, you could do:
var custId = "1013686356";
var determineIfContacted = function(custId, data) {
var customers = data.filter(function(item) { return item.ent_cust_id == custId; })
var customer = customers[0]; // you should validate that you have a matching customer here
var hasBeenContacted = false;
foreach (var offerId in customer.offers) {
if (customer.offers[offerId].contacted === "Y") {
hasBeenContacted = true;
break;
}
}
return hasBeenContacted;
}
Hope this helps (:
Upvotes: 1