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

My bad, I got the charCode wrong it should be 64 ๐Ÿ˜€

$group.map((g,i)=> $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1) instanceof Object ? g.mapValues((val,key)=> $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1)[โ€˜colโ€™].find((x,y,z)=> x[โ€˜@idโ€™] == (key.charCodeAt(0) - 64)) instanceof Object ? $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1)[โ€˜colโ€™].find((x,y,z)=> x[โ€˜@idโ€™] == (key.charCodeAt(0) - 64))[โ€˜$โ€™] : val ) : g )

image

rashmi
New Contributor III

hi @alchemiz the column keys in group array maynot be characters like A,B,Cโ€ฆ its suppose to map based on the position like col1,col2 etc the expression suggested by @bojanvelevski
$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)
works irrespective of column name in group array but its should be tweaked to satisfy my input becoz in which he had metioned $[โ€˜@idโ€™] comes in as filed but in my case it comes in as an array element like this jsonPath($, โ€œrow[][โ€˜@idโ€™]") and also $col comes in as an array but in my case it comes in under row array like this jsonPath($, "row[].colโ€), so i need help in adjusting the above expression to satisfy my input.Please could u help ๐Ÿ˜”

Just replace the criteria when checking for the col objectโ€ฆ

$group.map((g,i)=> $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1) instanceof Object ? g.mapValues((val,key)=> $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1)[โ€˜colโ€™].find((x,y,z)=> x[โ€˜@idโ€™] == (g.keys().indexOf(key) + 1)) instanceof Object ? $row.find((a,b,c)=> a[โ€˜@idโ€™] == i + 1)[โ€˜colโ€™].find((x,y,z)=> x[โ€˜@idโ€™] == (g.keys().indexOf(key) + 1)).get(โ€˜$โ€™,val) : val ) : g )

image

rashmi
New Contributor III

@alchemiz thanks a lot!! its working ๐Ÿ™‚

Glad to be of help ๐Ÿค˜