我有一个简单的nodejs get,运行方式如下:
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')});
};
});
});
并将结果返回到这样的数组中:
{"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"}
}
}
我知道如何获得数组的第一级,但是如果我想获得数组的任何其他部分该怎么办。
如果我还要检查offers
中的contacted
字段是否在{hh offer部分的任何结果中被标记为Y
,该怎么办?
我该怎么做?
编辑:
看起来这只是一个字符串而不是数组。那么也许问题是,如何将其转换为数组和/或json对象?
答案 0 :(得分:1)
首先,我没有看到任何阵列,所以我对第一个问题有点困惑?
但是在回答你的第二个问题时,如果你的数据被评估成一个名为'data'的对象,这是一个上述JSON的数组,你可以这样做:
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;
}
希望这会有所帮助(: