0

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]"     
      }
] 

2 Answers 2

0

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]"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.