cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Replace value according to its poition

KTsnap
New Contributor III

Below is the Input
[
{
โ€œ@idโ€: โ€œ2โ€,
โ€œcolโ€: [
{
โ€œ@idโ€: โ€œ2โ€,
โ€œ@transableโ€: โ€œyesโ€,
โ€œ$โ€: โ€œReplace1โ€
},
{
โ€œ@idโ€: โ€œ3โ€,
โ€œ@transableโ€: โ€œnoโ€,
โ€œ$โ€: โ€œReplace2โ€
}
],
โ€œgroupโ€: [
{
โ€œAโ€: โ€œA1โ€,
โ€œBโ€: โ€œB1โ€,
โ€œCโ€: โ€œC1โ€,
โ€œDโ€: โ€œD1โ€,
โ€œEโ€: "E1 ",
โ€œFโ€: โ€œF1โ€,
โ€œGโ€: โ€œG1โ€,
โ€œHโ€: โ€œโ€,
โ€œIโ€: โ€œโ€
},
{
โ€œAโ€: โ€œA2โ€,
โ€œBโ€: โ€œB2โ€,
โ€œCโ€: โ€œC2โ€,
โ€œDโ€: โ€œD2โ€,
โ€œEโ€: โ€œโ€,
โ€œFโ€: โ€œF2โ€,
โ€œGโ€: โ€œG2โ€,
โ€œHโ€: โ€œโ€,
โ€œIโ€: โ€œโ€
}
]
}
]


expected Output :

[
  {
    "group": [
      {
        "A": "A1",
        "B": "B1",
        "C": "C1",
        "D": "D1",
        "E": "E1 ",
        "F": "F1",
        "G": "G1",
        "H": "",
        "I": ""
      },
      {
        "A": "A2",
        "B": "Replace1",
        "C": "Replace2",
        "D": "D2",
        "E": "",
        "F": "F2",
        "G": "G2",
        "H": "",
        "I": ""
      }
         ]
  }
]

explanantion : @id refers to row number of the group and @id under col refers to the column number of the group where replacement needs to be done with  the value  of "$"
14 REPLIES 14

bojanvelevski
Valued Contributor

Hi @KTsnap,

I believe this expression will do the job:

$group.map((x,index)=>$['@id'] == index + 1 ? (x.mapValues((v,k)=> $col.find(e => e['@id']-1 == x.keys().indexOf(k)) != null ? $col.find(e => e['@id']-1 == x.keys().indexOf(k) ).get('$') : v)) : x)

Itโ€™s rather complex, so I would suggest you to do some testing.

Regards,
Bojan

rashmi
New Contributor III

Hi @bojanvelevski similarly if i have two array called row and group as shown below
inputcommunity
firstarray
secondarray
I need to replace the the value of second array ie group array with values from first array ie row array
to elaborate->row id in first array (row array) is corresponding to first record in second array(group array) and colid in first array(row array) is corresponding to A, B, C,D,E,F,G,H,I in the second array(group array).Example:first record of second array(group array) with values corresponding to column B and C must be updated with value ($) corresponding to first array(row array) with row id 1 and col id is 2 &col id is 3but remaining column values like A,D,E,Fโ€ฆI values should remain as incoming input values

Expected output is as shown below
outputcommunity

Please could u help telling how can we achieve this using mapper expression here

If my understanding is correct, this is the same situation, isnโ€™t it ?

You just need to tweak the expression a little bit.

rashmi
New Contributor III

Hi @bojanvelevski yes but here id corresponding to row comes in an array so how can we change it accordingly?
i tried this way its not working pls could u help if am missing something or doing wrong pls
expr
input.json (1.0 KB)
@bojanvelevski, @alchemiz please could u help ๐Ÿ˜”