I have the JSON as following ,
var json =
[
{"cardAid":"ALPHG","displayName":"ALPHG","SequenceNumber":"0","port":"A"},
{"cardAid":"ALPHG","displayName":"ALPHG","SequenceNumber":"1","port":"B"},
{"cardAid":"SFD","displayName":"SFD","SequenceNumber":"2","port":"C"},
{"cardAid":"SFD","displayName":"SFD","SequenceNumber":"3","port":"D"},
{"cardAid":"ALPHG","displayName":"ALPHG","SequenceNumber":"4","port":"E"},
{"cardAid":"ALPHG","displayName":"ALPHG","SequenceNumber":"5","port":"F"},
{"cardAid":"BETA","displayName":"BETA","SequenceNumber":"6","port":"G"}
];
I used the code to parse this JSON from this link, TO Get the number of Count from JSON in javascript?
function count(arr) {
var map = {};
for (var i = 0; i < arr.length; i++) {
var name = arr[i].cardAid;
var port = arr[i].port;
if (name in map){
map[name].Count++;
map[name].Port += ',' + port;
} else {
map[name] = { Count: 1, Port: port };
}
}
return map;
}
This Function parses the JSON using cardAid as the key and gives output as,
{
ALPHG: { Count: 4, Port: "A,B,E,F" },
SFD: { Count: 2, Port: "C,D" },
BETA: { Count: 1, Port: "G" }
}
But i need the output based on the SequenceNumber in the JSON,
{
ALPHG: { Count: 2, Port: "A,B" },
SFD: { Count: 2, Port: "C,D" },
ALPHG: { Count: 2, Port: "E,F" },
BETA: { Count: 1, Port: "G" }
}
How can i put this duplicate name in the Map based on the SequenceNumber from JSON
ALPHGobject. Furthermore, the desired output isn't valid because you can't have two object properties with the same name. They'll just overwrite one another, or in the case of you parser, it looks like it is concatenating them together.