endtoend6
endtoend6

Reputation: 75

Loop through an object of arrays and construct an object

I have a complex object and using that Im trying to create a single object with all the options. The structure that I have right now.

model = {
  "questionnaires": [
    {
      "number": "377",
      "self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/377",
      "version": "V2",
      "categoryCode": "103",
      "typeCode": "201",
      "questionAndAnswerOptions": [
        {
          "questionCode": "q0101",
          "questionTypeCode": "100",
          "answerOptionCodes": [
            "010101",
            "010102",
            "010103"
          ],
          "questionCodeDes": " cat 1 question 1",
          "answerOptionCodesDes": {
            "010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
            "010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
            "010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim"
          }
        },
        {
          "questionCode": "q0102",
          "questionTypeCode": "200",
          "answerOptionCodes": [
            "010201",
            "010202",
            "010203"
          ],
          "questionCodeDes": "cat 1 question 2",
          "answerOptionCodesDes": {
            "010201": "Option1",
            "010202": "Option2",
            "010203": "Option3"
          }
        }
      ],
      "extension": {
        "contractNumber": "43385",
        "accountNumber": "MP373697",
        "product": {
          "number": "378",
          "typeCode": "100",
          "nameCode": "201"
        }
      }
    },
    {
      "number": "394",
      "self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/394",
      "version": "V2",
      "categoryCode": "108",
      "typeCode": "205",
      "questionAndAnswerOptions": [
        {
          "questionCode": "q0201",
          "questionTypeCode": "300",
          "answerOptionCodes": [
            "020101",
            "020102",
            "020103",
            "020104",
            "020105"
          ],
          "questionCodeDes": "cat 2 ques 1",
          "answerOptionCodesDes": {
            "020101": "80-100% arasinda",
            "020102": "60-80% arasinda",
            "020103": "40-60%? arasinda",
            "020104": "20-40%? arasinda",
            "020105": "20% ve daha azini"
          }
        },
        {
          "questionCode": "q0202",
          "questionTypeCode": "400",
          "answerOptionCodes": [
            "020201",
            "020202",
            "020203",
            "020204",
            "020205"
          ],
          "questionCodeDes": "cat 2 ques 2",
          "answerOptionCodesDes": {
            "020201": "80-100% arasinda",
            "020202": "60-80% arasinda",
            "020203": "40-60%? arasinda",
            "020204": "20-40%? arasinda",
            "020205": "20% ve daha azini"
          }
        },
        {
          "questionCode": "q0203",
          "questionTypeCode": "500",
          "answerOptionCodes": [
            "020301",
            "020302",
            "020303",
            "020304",
            "020305"
          ],
          "questionCodeDes": "cat 2 ques 3",
          "answerOptionCodesDes": {
            "020301": "80-100% arasinda",
            "020302": "60-80% arasinda",
            "020303": "40-60%? arasinda",
            "020304": "20-40%? arasinda",
            "020305": "20% ve daha azini"
          }
        }
      ],
      "extension": {
        "contractNumber": "63271",
        "accountNumber": "MP373697",
        "product": {
          "number": "395",
          "typeCode": "100",
          "nameCode": "102"
        }
      }
    }
  ]
}

Required Output:

   {
    "010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
                "010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
                "010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim",
                 "010201": "Option1",
                "010202": "Option2",
                "010203": "Option3",
                "020101": "80-100% arasinda",
                "020102": "60-80% arasinda",
                "020103": "40-60%? arasinda",
                "020104": "20-40%? arasinda",
                "020105": "20% ve daha azini",
                "020201": "80-100% arasinda",
                "020202": "60-80% arasinda",
                "020203": "40-60%? arasinda",
                "020204": "20-40%? arasinda",
                "020205": "20% ve daha azini",
                "020301": "80-100% arasinda",
                "020302": "60-80% arasinda",
                "020303": "40-60%? arasinda",
                "020304": "20-40%? arasinda",
                "020305": "20% ve daha azini"
}

All Im trying to do is collect all the answerOptionCodesDes and put it in a single object.

Please help, Thanks

Upvotes: 0

Views: 45

Answers (2)

trincot
trincot

Reputation: 350272

You could use reduce to return that result:

let result = model.questionnaires.reduce( 
    (acc, q) => q.questionAndAnswerOptions.reduce( 
        (acc, o) => Object.assign(acc, o.answerOptionCodesDes), acc), {});

let model = {
  "questionnaires": [
    {
      "number": "377",
      "self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/377",
      "version": "V2",
      "categoryCode": "103",
      "typeCode": "201",
      "questionAndAnswerOptions": [
        {
          "questionCode": "q0101",
          "questionTypeCode": "100",
          "answerOptionCodes": [
            "010101",
            "010102",
            "010103"
          ],
          "questionCodeDes": " cat 1 question 1",
          "answerOptionCodesDes": {
            "010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
            "010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
            "010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim"
          }
        },
        {
          "questionCode": "q0102",
          "questionTypeCode": "200",
          "answerOptionCodes": [
            "010201",
            "010202",
            "010203"
          ],
          "questionCodeDes": "cat 1 question 2",
          "answerOptionCodesDes": {
            "010201": "Option1",
            "010202": "Option2",
            "010203": "Option3"
          }
        }
      ],
      "extension": {
        "contractNumber": "43385",
        "accountNumber": "MP373697",
        "product": {
          "number": "378",
          "typeCode": "100",
          "nameCode": "201"
        }
      }
    },
    {
      "number": "394",
      "self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/394",
      "version": "V2",
      "categoryCode": "108",
      "typeCode": "205",
      "questionAndAnswerOptions": [
        {
          "questionCode": "q0201",
          "questionTypeCode": "300",
          "answerOptionCodes": [
            "020101",
            "020102",
            "020103",
            "020104",
            "020105"
          ],
          "questionCodeDes": "cat 2 ques 1",
          "answerOptionCodesDes": {
            "020101": "80-100% arasinda",
            "020102": "60-80% arasinda",
            "020103": "40-60%? arasinda",
            "020104": "20-40%? arasinda",
            "020105": "20% ve daha azini"
          }
        },
        {
          "questionCode": "q0202",
          "questionTypeCode": "400",
          "answerOptionCodes": [
            "020201",
            "020202",
            "020203",
            "020204",
            "020205"
          ],
          "questionCodeDes": "cat 2 ques 2",
          "answerOptionCodesDes": {
            "020201": "80-100% arasinda",
            "020202": "60-80% arasinda",
            "020203": "40-60%? arasinda",
            "020204": "20-40%? arasinda",
            "020205": "20% ve daha azini"
          }
        },
        {
          "questionCode": "q0203",
          "questionTypeCode": "500",
          "answerOptionCodes": [
            "020301",
            "020302",
            "020303",
            "020304",
            "020305"
          ],
          "questionCodeDes": "cat 2 ques 3",
          "answerOptionCodesDes": {
            "020301": "80-100% arasinda",
            "020302": "60-80% arasinda",
            "020303": "40-60%? arasinda",
            "020304": "20-40%? arasinda",
            "020305": "20% ve daha azini"
          }
        }
      ],
      "extension": {
        "contractNumber": "63271",
        "accountNumber": "MP373697",
        "product": {
          "number": "395",
          "typeCode": "100",
          "nameCode": "102"
        }
      }
    }
  ]
};

let result = model.questionnaires.reduce( 
    (acc, q) => q.questionAndAnswerOptions.reduce( 
        (acc, o) => Object.assign(acc, o.answerOptionCodesDes), acc), {});

console.log(result);

Upvotes: 3

Thomas Altmann
Thomas Altmann

Reputation: 1744

First you define the object you want all the values in:

var mapped = {};

Then loop over every entry in the model-array, and in that loop, loop again through every entry in the questionAndAnswerOptions-array. Then you can add the objects in those arrays to the mapped-variable with Object.assign

model.questionnaires.forEach(function(questionnaire){
  questionnaire.questionAndAnswerOptions.forEach(function(qAndA){
    Object.assign(mapped,qAndA.answerOptionCodesDes);
  });
});

Upvotes: 3

Related Questions