Compare two Arrays and remove the block from one array if certain data exists in both arrays

79 Views Asked by At

I need to compare two arrays that have different data fields in them. The comparison is based on lets say Id number and if data matches in the two arrays, I need to remove the whole block/row from the array. For example, in the below arrays, I need to compare OrderId(from Array1) and SystemId(from Array2) and if the Ids match the whole block needs to be removed from Array1. Please see sample arrays and output below:

Array1 = [
  {
    "OrderId": "00111111",
    "BillingCountry": "GB",
    "CurrencyIsoCode": "GBP",
    "PersonEmail": "[email protected]"   
  },
  {
    "OrderId": "00222222",
    "BillingCountry": "US",
    "CurrencyIsoCode": "USD",
    "PersonEmail": "[email protected]"     
  }
] 

Array2 = [
  {
    "SystemId": "00111111" 
  },
  {
    "SystemId": "00333333" 
  },
  {
    "SystemId": "00444444" 
  }
] 

Output:   [
    {
        "OrderId": "00222222",
        "BillingCountry": "US",
        "CurrencyIsoCode": "USD",
        "PersonEmail": "[email protected]"     
      }
] 
3

There are 3 best solutions below

1
On

I believe for these kind of problem using the Arrays module some() function express the intention clearly.

%dw 2.0
output application/json
import some from dw::core::Arrays

var Array1 = [
  {
    "OrderId": "00111111",
    "BillingCountry": "GB",
    "CurrencyIsoCode": "GBP",
    "PersonEmail": "[email protected]"   
  },
  {
    "OrderId": "00222222",
    "BillingCountry": "US",
    "CurrencyIsoCode": "USD",
    "PersonEmail": "[email protected]"     
  }
] 

var Array2 = [
  {
    "SystemId": "00111111" 
  },
  {
    "SystemId": "00333333" 
  },
  {
    "SystemId": "00444444" 
  }
] 
---
Array1 filter ((item) -> not (Array2 some ($.SystemId == item.OrderId)))

Output

[
  {
    "OrderId": "00222222",
    "BillingCountry": "US",
    "CurrencyIsoCode": "USD",
    "PersonEmail": "[email protected]"
  }
]
0
On

Simply done, like below:

%dw 2.0
output application/json
import * from dw::core::Arrays
var Array1 = [
  {
    "OrderId": "00111111",
    "BillingCountry": "GB",
    "CurrencyIsoCode": "GBP",
    "PersonEmail": "[email protected]"   
  },
  {
    "OrderId": "00222222",
    "BillingCountry": "US",
    "CurrencyIsoCode": "USD",
    "PersonEmail": "[email protected]"     
  }
] 

var Array2 = [
  {
    "SystemId": "00111111" 
  },
  {
    "SystemId": "00333333" 
  },
  {
    "SystemId": "00444444" 
  }
] 

---
Array1 filter !(Array2.SystemId contains $.OrderId)
0
On

This DataWeave script uses the map function to extract all the SystemId values from Array2. Then, it filters Array1 based on whether the OrderId is contained within the systemIds array. This should exclude elements from Array1 where the OrderId matches any SystemId in Array2.

%dw 2.0
var Array2 = [
  {
    "SystemId": "00111111"
  },
  {
    "SystemId": "00333333"
  },
  {
    "SystemId": "00444444"
  }
]
var Array1 = [
  {
    "OrderId": "00111111",
    "BillingCountry": "GB",
    "CurrencyIsoCode": "GBP",
    "PersonEmail": "[email protected]"
  },
  {
    "OrderId": "00222222",
    "BillingCountry": "US",
    "CurrencyIsoCode": "USD",
    "PersonEmail": "[email protected]"
  }
]


output application/json
---

Array1 filter ((item1) -> not ((Array2 map ((item) -> item.SystemId)) contains item1.OrderId))