这是我的代码,其中我从wcf服务获取所有数据并将该数据存储到带密钥的字典中。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
var parseString = require('xml2js').parseString;
var BasicHttpBinding = require('wcf.js').BasicHttpBinding
, Proxy = require('wcf.js').Proxy
, binding = new BasicHttpBinding(
{
})
, proxy = new Proxy(binding, "http://localhost:9877/IDBReport/basicHttp")
, message = "<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<Header />" +
"<Body>" +
"<OrderReport xmlns='http://tempuri.org/'>" +
"<value></value>" +
"<value></value>" +
"<value>+ req.query.start +</value>" +
"<value>+ req.query.end +</value>" +
"<value></value>" +
"<value></value>" +
"<value></value>" +
"<value></value>" +
"<value></value>" +
"</OrderReport>" +
"</Body>" +
"</Envelope>";
proxy.send(message, "http://tempuri.org/IDBReport/OrderReport", function (message, ctx) {
app.set("view engine", "jade");
app.get('/', function (req, res) {
parseString(message, function (err, result) {
var response = result['s:Envelope']['s:Body'][0].OrderReportResponse[0].obj[0];
var marketOrder = response['a:MarketOrder'];
var dict1 = [];
for (var i = 0; i < marketOrder.length; i++) {
var keys1 = [];
var keys = [];
keys.push({
k1: marketOrder[i]['a:SecurityExchange'],
k2: marketOrder[i]['a:Symbol'],
k3: marketOrder[i]['a:MaturityMonthYear']
});
if (keys1.indexOf(keys[0].k1) && keys1.indexOf(keys[0].k2) && keys1.indexOf(keys[0].k3)) {
keys1.push(keys[0].k1, keys[0].k2, keys[0].k3);
}
for (var k1 in keys1[0]) {
var dict = {};
dict[keys1] = marketOrder[i];
if (dict1.indexOf(dict)) {
dict1.push(dict);
}
}
}
for (var i = 0; i < dict1.length; i++) {
for(var t in dict1[i]){
console.log(dict1[i]); //In 'dict1' all data is storing
}}
// var serialized = JSON.stringify(dict1, null, 4);
// console.log(serialized);
res.render('MarketPosition', {mrkt: marketOrder});
});
});
});
var server = app.listen(3000, function () {
console.log('Server is running..http://localhost:3000/');
});
现在我想比较所有键,并且只想打印那些不按键重复的记录(意味着键值相同,不应该打印数据)我该怎么做。< / p>
{ 'CME,ES,201703':
{
'a:CustomerOrFirm': [ 'Customer' ],
'a:MaturityMonthYear': [ '201703' ],
'a:Modifier': [ '' ],
'a:MsgSeqNumber': [ '0' ],
'a:MsgType': [ 'D' ],
'a:OrdType': [ 'Limit' ],
'a:SecurityExchange': [ 'CME' ],
'a:SecurityId': [ [Object] ],
'a:SecurityType': [ 'FUT' ],
'a:SenderCompId': [ 'TRDFIRSTDTS' ],
'a:SendingTime': [ '2016-12-20T11:44:05' ],
'a:Symbol': [ 'ES' ],
'a:TTOrderId': [ '' ],
'a:TargetCompId': [ 'TTDEV25O' ],
'a:TimeInForce': [ '0' ],
'a:TmpOrderQty': [ '0' ],
'a:TransactTime': [ '2016-12-20T11:44:04' ] } }
{ 'CME,ZB,201703':
{ 'a:CustomerOrFirm': [ 'Customer' ],
'a:MaturityMonthYear': [ '201703' ],
'a:Modifier': [ '' ],
'a:MsgSeqNumber': [ '0' ],
'a:MsgType': [ 'D' ],
'a:OrdType': [ 'Market' ],
'a:SecurityExchange': [ 'CME' ],
'a:SecurityId': [ [Object] ],
'a:SecurityType': [ 'FUT' ],
'a:SenderCompId': [ 'TRDFIRSTDTS' ],
'a:SendingTime': [ '2016-12-20T11:44:52' ],
'a:Symbol': [ 'ZB' ],
'a:TTOrderId': [ '' ],
'a:TargetCompId': [ 'TTDEV25O' ],
'a:TimeInForce': [ '-' ],
'a:TmpOrderQty': [ '0' ],
'a:TransactTime': [ '2016-12-20T11:44:50' ]} }
{ 'CME,ZB,201703':
{ 'a:CustomerOrFirm': [ 'Customer' ],
'a:MaturityMonthYear': [ '201703' ],
'a:Modifier': [ '' ],
'a:MsgSeqNumber': [ '0' ],
'a:MsgType': [ 'D' ],
'a:OrdType': [ 'Market' ],
'a:SecurityExchange': [ 'CME' ],
'a:SecurityId': [ [Object] ],
'a:SecurityType': [ 'FUT' ],
'a:SenderCompId': [ 'TRDFIRSTDTS' ],
'a:SendingTime': [ '2016-12-20T11:44:52' ],
'a:Symbol': [ 'ZB' ],
'a:TTOrderId': [ '' ],
'a:TargetCompId': [ 'TTDEV25O' ],
'a:TimeInForce': [ '-' ],
'a:TmpOrderQty': [ '0' ],
'a:TransactTime': [ '2016-12-20T11:44:50' ] } }
我得到的输出如上所述。
答案 0 :(得分:0)
将使用过的密钥推入数组,然后在循环浏览字典时,检查数组是否包含密钥,如果是,则不要打印它。
var dictionary = [{ Key: 'A', Value: 'Key 1'}, { Key: 'A', Value: 'Key 1'}, { Key: 'B', Value: 'Key 2'}, { Key: 'B', Value: 'Key 2'}, { Key: 'C', Value: 'Key 3'}];
var usedKeys = [];
for (var i = 0; i < dictionary.length; i++) {
if (usedKeys.indexOf(dictionary[i].Key) == -1) {
console.log(dictionary[i].Value);
}
usedKeys.push(dictionary[i].Key);
}
&#13;