1

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

9
  • 1
    Your desired output is missing a comma after the second ALPHG object. 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. Commented Sep 29, 2015 at 14:05
  • Corrected the desired output Commented Sep 29, 2015 at 14:07
  • Hey, I'm not sure what you want.... this out put could be possible if you limit the coun to 2 then group the port again in another, is that what you want? Commented Sep 29, 2015 at 14:12
  • 1
    @AlvaroJoao The second object property with the same name will overwrite the first. You can't have two properties with the same exact name. jsfiddle... Commented Sep 29, 2015 at 14:18
  • @War10ck I'm so sorry , was a typo on to -> not (really meant) thanks for the heads up! Commented Sep 29, 2015 at 14:20

1 Answer 1

3

This could be close to what you had in mind (jsfiddle), I opted to having Port as an array instead of a string. But I am a bit confused as SequenceNumber has no impact in this logic, maybe elements should be sorted first by SequenceNumber?

function count(data) {
    var result = {};

    for (var i = 0; i < data.length; ++i) {
        var cardAid = data[i].cardAid;

        if (!(cardAid in result)) {
            result[cardAid] = [];
        }

        // Check if a new "sequence" needs to be created
        if (i == 0 || data[i-1].cardAid != cardAid) {
            result[cardAid].push({ Count: 0, Port: [] });
        }

        // Append item to the latest sequence
        var lastIndex = result[cardAid].length - 1;
        ++result[cardAid][lastIndex].Count;
        result[cardAid][lastIndex].Port.push(data[i].port);
    }

    return result;
}

Result:

{
  "ALPHG": [
    {
      "Count": 2,
      "Port": [ "A", "B" ]
    },
    {
      "Count": 2,
      "Port": [ "E", "F" ]
    }
  ],
  "SFD": [
    {
      "Count": 2,
      "Port": [ "C", "D" ]
    }
  ],
  "BETA": [
    {
      "Count": 1,
      "Port": [ "G" ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what i needed.. But i don't want to Append item to the latest sequence. I want to create a New Sequence Ex:"ALPHG_1": [ { "Count": 2, "Port": [ "E", "F" ] },
An other try at jsfiddle has object keys "ALPHG_0", "SFD_0", "ALPHG_1", "BETA_0". But I imagine this would be trickier to use in other parts of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.