Update Array of nested list

Hello community,

I have the following scenario,

I have a array of student details. It has fields like student id, subject id, question paper id. I want to group by subject id and update the question paper id in the array.

Given Array 1:

{student id: 100, subject id: "S1", question paper id:   },

{student id: 200, subject id: "S2", question paper id:   },

{student id: 300, subject id: "S1", question paper id:   },

{student id: 400, subject id: "S2", question paper id:   }

{student id: 300, subject id: "S1", question paper id:   },

{student id: 300, subject id: "S3", question paper id:   },

Array 2:

{Q1,Q2,Q3}

Array 3 :

{ S1,S2,S3}

Expected Output:

{student id: 100, subject id: "S1", question paper id: "Q1"},

{student id: 200, subject id: "S2", question paper id: "Q2"},

{student id: 300, subject id: "S1", question paper id: "Q1"},

{student id: 400, subject id: "S2", question paper id: "Q2"}

{student id: 300, subject id: "S1", question paper id: "Q1"},

{student id: 300, subject id: "S3", question paper id: "Q3"},

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    a!forEach(

    items: array 1,

    expression:  */ if you have the cdt manipulation plugin /*

    updateCDT(

    fv!item,

    "questionPaperId",

    */ this uses the position in array 3 to find the right question in array 2 inline /*

    index (array 2, wherecontains( array 3, fv!item.subjectId), {})

    )

    */ if you don't have the plugin, but it is a CDT */

    type!CDTName(

    studentID: fv!item.studentID,

    subjectId: fv!item.subjectId,

    */ this uses the position in array 3 to find the right question in array 2 inline /*

    questionPaperId: index (array 2, wherecontains( array 3, fv!item.subjectId), {})

    )

    */ if it's not a cdt but a dictionary, and if you happen to be using a rule to get the ID from the subject ID /*

    { studentId: fv!item.studentId, subjectId: fv!item.subjectId, questionPaperId: rule!getQuestionPaperId(fv!item.subjectID) }

    )

    If you are expecting the possibility of nulls, you might make it a little more robust with index() or property() functions, but dot notation here for readability, and it will work with dot notation.

    You either run the updateCDT function, or you have each loop create a new CDT or new dictionary exactly like the old one aside from the changes.  You take the brand new set and save it over the old set, i.e. in the same place, and it's LIKE you updated it.

  • Hi

    When i used 

    type!CDTName(

    studentID: fv!item.studentID,

    subjectId: fv!item.subjectId,

    */ this uses the position in array 3 to find the right question in array 2 inline /*

    questionPaperId: index (array 2, wherecontains( array 3, fv!item.subjectId), {})

    )

    I got wrong output. I got "Q1" for all subject id. Please help me to resolve this.

    Output received:

    {student id: 100, subject id: "S1", question paper id: "Q1"},

    {student id: 200, subject id: "S2", question paper id: "Q1"},

    {student id: 300, subject id: "S1", question paper id: "Q1"},

    {student id: 400, subject id: "S2", question paper id: "Q1"}

    {student id: 300, subject id: "S1", question paper id: "Q1"},

    {student id: 300, subject id: "S3", question paper id: "Q1"},

  • 0
    Certified Lead Developer
    in reply to SangeethaBabu

    Have the solution simply output whereContains(array3, fv!item.subjectId).  Lets see if that works.

    What is should be doing each loop is seeing where "S1" is located in the array {"S1", "S2", "S3"} and returning 1,

    then seeing where "S2" is located in the array {"S1", "S2", "S3"} and returning 2,

    then seeing where "S1" is located in the array {"S1", "S2", "S3"} and returning 1,

    Make sure you're using wherecontains, not contains.  That returns a boolean whether the item is anywhere in the list at all or not.  That might be getting implicitly cast as an integer, which would cause you to return the first if it's anywhere, 1 being equivalent of true.

    Check for typos.  You might be getting behavior for none of the subjects in your array being in the list of subjects at all.  Make sure the types are the same all around.

  •  

    I'm using wherecontains() and all the types are text but i still go the same output

Reply Children
No Data