Zak01
Zak01

Reputation: 17

Need to update values in an array by comparing two arrays and hard coded data

I need to compare two arrays and replace values if id is same and a particular field in first array is null or has some hard coded data(test). If this condition is not true, don't do anything and keep the original values. Below are the two sample arrays:

array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data",
        "newField": "test"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data2",
        "newField": null
      },
      {
        "Id": "test3",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data2",
        "newField": "dummy"
      }
    ]
array2 = = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"

      },
      {
        "Id": "test4",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
    ]

In these two arrays, if Id is same and newField from array1 is either null or has "test" then I need to replace "fname" and "lname" in array2 with hard coded "Matched" And if these both conditions are not matched then the original values will be used

logic is similar like this:

if array1.id == array2.id and (arrar1.newField == Null or arrar1.newField == "test") then replace fname and lname with "Matched" else keep whatever is in there. In the above example since test1 is matched I should get below output:

[
          {
            "Id": "test1",
            "Date": "2021-11-05T12:53:00.000Z",
            "fname": "Matched",
            "lname": "Matched"

          },
          {
            "Id": "test4",
            "Date": "2021-11-05T12:53:00.000Z",
            "fname": "Chris",
            "lname": "Smith"
          }
        ]

Upvotes: 0

Views: 334

Answers (2)

sudhish_s
sudhish_s

Reputation: 628

Creates a map for Array1 elements by Id to reduce the number of iterations through the Array1. I assumed that there can be multiple records with same id in Array1.

%dw 2.0
output application/json
var array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data",
        "newField": "test"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data2",
        "newField": null
      },
      {
        "Id": "test3",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data2",
        "newField": "dummy"
      }
    ]
var array2 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"

      },
      {
        "Id": "test4",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
    ]

var array1byId = array1 groupBy ((item) -> item.Id)

---
array2 map ((item) -> do {
    var array1Item = array1byId [item.Id] default []
    var newFields = array1Item filter ($.newField == null or $.newField == "test")
    ---
    if ( (not isEmpty (array1Item))
                and (sizeOf (newFields) > 0))                    
        item update {
            case ."fname" -> "Matched"
            case ."lname" -> "Matched"
        }
    else item
    
})

Upvotes: 0

Anurag Sharma
Anurag Sharma

Reputation: 935

You can try this Script where we are iterating the second array and cheking if its ID field value matching with first array ID field Value then updating the fname and lname

%dw 2.0
import * from dw::util::Values
var array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data2"
      }
    ]
var array2 =  [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"

      },
      {
        "Id": "test3",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
    ]
output application/json  
---
array2 map ((item,index) -> 
if(array1.Id contains item.Id)
((item update "fname" with "Matched") update "lname" with "Matched") else item)

enter image description here

Upvotes: 2

Related Questions