skip to Main Content

I am new to Javascript, so need some help with the below scenario.

I am reading a JSON input as shown below

var json_input = "[
    {
        "src_AccountId":["00100027H3QAK"],
        "src_AccountName":["Vis Card Services","Vitoria Card Services SA"],
        "src_MaxVus":[15,3],
        "src_DocCount":[15,3]
    },
    {
        "src_AccountId":["0010J000027lVFDQA2","6516983c9d71be4bcd799313"],
        "src_AccountName":["Semiconductor Components","asdadasda"],
        "src_MaxVus":[880,50],
        "src_DocCount":[19,10]
    }]";

How can I split & concatenate the input JSON data in order to get the below output? So basically I want 4 arrays each for AccountId, AccountName, MaxVus and DocCount. Also if there is more than 1 accountname for a particular accountid then I want only one accountname to be included in the result based on account which has highest value for MaxVus.

[
    {
        "AccountId": [
            "00100027H3QAK",
            "0010J000027lVFDQA2",
            "6516983c9d71be4bcd799313"
        ],
        "AccountName": [
            "Vis Card Services",
            "Semiconductor Components",
            "asdadasda"
        ],
        "DocCount": [
            "15",
            "19",
            "10"
        ],
        "MaxVus": [
            "15",
            "880",
            "50"
        ]
    }
]

Thanks

2

Answers


  1. You need to parse the JSON first, then iterate over the objects within it, and construct the output in the structure you want.

    Demo:

    var json_input = "[{"src_AccountId":["00100027H3QAK"],"src_AccountName":["Vis Card Services","Vitoria Card Services SA"],"src_MaxVus":[15,3],"src_DocCount":[15,3]},{"src_AccountId":["0010J000027lVFDQA2","6516983c9d71be4bcd799313"],"src_AccountName":["Semiconductor Components","asdadasda"],"src_MaxVus":[880,50],"src_DocCount":[19,10]}]";
    
    var data = JSON.parse(json_input); //parse the JSON input
    var output = {
      "AccountId": [],
      "AccountName": [],
      "DocCount": [],
      "MaxVus": []
      
    };
    
    //iterate over the objects
    for (var i = 0; i < data.length; i++) {
      var src_AccountId = data[i].src_AccountId;
      var src_AccountName = data[i].src_AccountName;
      var src_MaxVus = data[i].src_MaxVus;
      var src_DocCount = data[i].src_DocCount;
    
      for (var j = 0; j < src_AccountId.length; j++) {
        //if the AccountId is not already in the output, add it along with the corresponding values
        if (!output.AccountId.includes(src_AccountId[j])) {
          output.AccountId.push(src_AccountId[j]);
          output.AccountName.push(src_AccountName[j]);
          output.MaxVus.push(src_MaxVus[j]);
          output.DocCount.push(src_DocCount[j]);
        } else {
          //if the AccountId is already in the output, update values based on MaxVus
          var index = output.AccountId.indexOf(src_AccountId[j]);
          if (src_MaxVus[j] > output.MaxVus[index]) {
            output.AccountName[index] = src_AccountName[j];
            output.MaxVus[index] = src_MaxVus[j];
            output.DocCount[index] = src_DocCount[j];
          }
        }
      }
    }
    
    //convert the values to strings (as per your example)
    output.MaxVus = output.MaxVus.map(String);
    output.DocCount = output.DocCount.map(String);
    
    //put the result in an array
    var result = [output];
    console.log(JSON.stringify(result, null, 2)); //output the result
    Login or Signup to reply.
  2. You can try:

    const json_input = `[{
            "src_AccountId":["00100027H3QAK"],
            "src_AccountName":["Vis Card Services","Vitoria Card Services SA"],
            "src_MaxVus":[15,3],
            "src_DocCount":[15,3]
        },
        {
            "src_AccountId":["0010J000027lVFDQA2","6516983c9d71be4bcd799313"],
            "src_AccountName":["Semiconductor Components","asdadasda"],
            "src_MaxVus":[880,50],
            "src_DocCount":[19,10]
        }]`;
    
    const data = JSON.parse(json_input);
    
    const AccountId = [];
    const AccountName = [];
    const DocCount = [];
    const MaxVus = [];
    
    for (let obj of data) {
      for (let i = 0; i < obj.src_AccountId.length; i++) {
        const index = AccountId.indexOf(obj.src_AccountId[i]);
        if (index === -1) {
          AccountId.push(obj.src_AccountId[i]);
          AccountName.push(obj.src_AccountName[i]);
          DocCount.push(obj.src_DocCount[i]);
          MaxVus.push(obj.src_MaxVus[i]);
        } else {
    
          if (obj.src_MaxVus[i] > MaxVus[index]) {
            AccountName[index] = obj.src_AccountName[i];
            MaxVus[index] = obj.src_MaxVus[i];
          }
        }
      }
    }
    
    const output = [{
      AccountId,
      AccountName,
      DocCount,
      MaxVus
    }];
    
    console.log(output);

    Code Explanation:

    1. Parse the JSON input into a JavaScript object.
    2. Define variables to store result.
    3. Iterate over the parsed Object.
    4. If the AccountId doesn't exist in the AccountId array, or the new MaxVus is greater, add it and the other values
    5. If the AccountId already exists in the AccountId array, remove it and its associated values
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search