Arcade Expression - Calculate Field - Reorganizing data within a field

251
4
Jump to solution
03-27-2024 09:02 AM
Labels (1)
Roose_22
New Contributor

I'm creating a grid index for roads. I've created the field (Page:Grid) and it includes the page number and grid number, but they are listed as such: 

esriCommunityMarch24.JPG

I'd like the data to display with the page number then all grids on that particular page. So rather than reading 26: C1, 26: C2, 26: C3, 26: C4 it would appear 26: C1, C2, C3, C4. I was thinking calculate field would be the best way to reorganize the field the way I'd like. Is this possible?

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This should give you the correct output

var arrField = Split($feature['Page:Grid'],",");
var dict = {}
for (var i in arrField) {
  var input = Split(arrField[i], ": ");
  var key = input[0];
  if (HasKey(dict, key)) {
    var value = Concatenate([dict[key], input[1]], ", ")
    dict[key] = value;
  } else {
    dict[key] = input[1]
  }
}
var output = [];
for (var k in dict) {
  Push(output, Concatenate([k, dict[k]], ': '))
}
return Concatenate(output, ' | ')

For example, this would return "20: E5 | 21: A5 | 22: A1 | 26: D1, E1" for row 8

View solution in original post

0 Kudos
4 Replies
Robert_Houston
Esri Contributor

Does the following calculate field script produce the output you'd like? Be sure to enable Edit Undo! You might need to change the $feature['x'] names to match your fields.

 

var pagenum = $feature['Page Number'] + ":"
pagenum + Concatenate(Split($feature['Page:Grid'],pagenum))

 

 

0 Kudos
Roose_22
New Contributor

Thanks, Robert! It did for some records, but not all for some reason.  I'm not sure if the attached image is ledgable or not. The field showing 39: A3,39: A4,39: A5,39: B2,39: B3,39: C2,38: E4,38: E5,42: B1,42: C1 produced results of 39: A3, A4, A5, B2, B3, C2,38: E4,38: E5,42: B1,42: C1 with the provided script. I created a new field named Index for the calculate field results to populate. 

esriCommunityMarch24_2.JPG

0 Kudos
KenBuja
MVP Esteemed Contributor

This should give you the correct output

var arrField = Split($feature['Page:Grid'],",");
var dict = {}
for (var i in arrField) {
  var input = Split(arrField[i], ": ");
  var key = input[0];
  if (HasKey(dict, key)) {
    var value = Concatenate([dict[key], input[1]], ", ")
    dict[key] = value;
  } else {
    dict[key] = input[1]
  }
}
var output = [];
for (var k in dict) {
  Push(output, Concatenate([k, dict[k]], ': '))
}
return Concatenate(output, ' | ')

For example, this would return "20: E5 | 21: A5 | 22: A1 | 26: D1, E1" for row 8

0 Kudos
Roose_22
New Contributor

Thank you!!

0 Kudos