简体   繁体   English

如何通过将其与对象数组中另一个字段的索引进行比较来获取值

[英]How to get value by comparing it with index of another field in the array of objects

If the text we are looking for is "PQR" then we should get the corresponding subfield value "dataweave".如果我们要查找的文本是“PQR”,那么我们应该得到相应的子字段值“dataweave”。 In the first object, we have the value but in the second object that is missing so it should give default null.在第一个对象中,我们有值,但在第二个对象中丢失了,所以它应该给默认 null。 Also the text will not be in order.文本也将不按顺序排列。 "ABC","BCD","PQR" are static in output even though it might come or not come in the input. “ABC”、“BCD”、“PQR”在输出中是静态的,即使它可能会出现或不会出现在输入中。

Input:输入:

 [ {
            "data":{
            "field": "value"
            },
            "field1": "ABC",
            "subfield01": "test",
            "field2": "BCD",
            "subfield02": "mule",
            "field3": "PQR",
            "subfield3": "dataweave"
            .
            .
            .
            "field10":"",
            "subfield10": ""
    },
    {
            "data":{
            "field": "value"
            }
            "field1": "BCD",
            "subfield01": "testing",
            "field2": "ABC",
            "subfield02": "mulesoft",
            "field3": "",
            "subfield03": ""
            .
            .
            .
            "field10":"",
            "subfield10": ""
    }
    ]

Expected Output:预期输出:

[
  {
    "SearchFieldABC": "test",
    "SearchFieldBCD": "mule",
    "SearchFieldPQR": "dataweave",
    "fields123": "value", 
    "fields678": "value2"
  },
  {
    "SearchFieldABC": "mulesoft",
    "SearchFieldBCD": "testing",
    "SearchFieldPQR": null,
    "fields123": "value", 
    "fields678": "value2"
  }
]

Though previous answer looks ok I post my虽然以前的答案看起来不错,但我发布了我的

%dw 2.0
output application/json
var query = ["ABC", "BCD", "PQR"]
---
payload map ((item, index) -> do { //Group by index
    var fieldByIndex = item 
        groupBy ((value, key) -> do {
            key as String match  {
                case f if(f startsWith "field") -> f[5 to -1]
                case f if(f startsWith "subfield") -> f[8 to -1]
            }
        })
     ---
     {
        (
            query 
                map ((q, index) -> do {
                    var fieldSubField = (fieldByIndex 
                                            filterObject ((value, key, index) -> value['field$(key)'] == q))[0] 
                                                            
                    ---
                    {
                        "SearchField$(q)": valuesOf(
                                                    fieldSubField 
                                                            filterObject ((value, key, index) -> key as String startsWith "subfield"))[0]  
                    }
                })
        )  
     }
} ) 

You can use the following dataweave.您可以使用以下数据编织。 I have divided it into two main functions, first mapFieldsToSubFields takes an element from your input and basically generates an object that directly maps the field's values to their respective subfield's value.我将它分为两​​个主要功能,第一个mapFieldsToSubFields从您的输入中获取一个元素,基本上生成一个对象,该对象直接将字段的值映射到它们各自的子字段的值。 So it will generate the following output for example因此它将生成以下输出,例如

{
  "ABC": "test",
  "BCD": "mule",
  "PQR": "dataweave"
}

This will make it is easier for the next function mapToRequiredOutput to quickly map it to required format and insert the missing fields from the input.这将使下一个函数mapToRequiredOutput更容易将其快速映射到所需格式并从输入中插入缺失的字段。

%dw 2.0
output application/json

var allFields = payload flatMap entriesOf($)
                    filter ($.key startsWith "field") and (!isEmpty($.value))
                    map $.value
                    distinctBy $

fun mapFieldsToSubFields(object) = 
    object filterObject ((value, key) -> (key startsWith "field")) // Remove all "subfields"
            filterObject !isEmpty($) // Remove empty field values
            mapObject ($): object["sub$($$)"] // example: $ = "ABC" and $$ = field1, so object["sub$($$)"] = object[subfield1] = "test"

fun mapToRequiredOutput(object) = {(
        allFields map {"SearchField$($)": object[$]}
    )}
---
payload 
    map mapFieldsToSubFields($)
    map mapToRequiredOutput($)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取对象数组中最大值的索引? - How to get index of the max value in array of objects? 如何通过使用 javascript 比较对象数组和 arrays 来获得不匹配的值 - how to get not matching value by comparing array of objects and arrays using javascript Python:通过从另一个数组比较从数组中提取索引值 - Python: Extract the index value from the array by comparing from another array 将多维数组中的值与另一个数组的索引进行比较 - Comparing the value from a multidimensional array with the index of another array Javascript通过将字符串值与数组进行比较来获取数组的索引值 - Javascript Get index value of array by comparing string value to array 如何通过使用另一个值的索引值获取数组值? - how to get array value by using index value of another value? 将键/值数组中的对象与另一个对象(键/值)进行比较 - Comparing objects from key/value array with another object (Key/value) Ramda:通过与另一个数组中的每个项目进行比较,从数组中获取对象 - Ramda: get objects from array by comparing with each item in another array 比较数组并在所选数组上获取另一个数组值 - Comparing array and get another array value on the selected array 如何通过比较值从嵌套对象数组中获取信息以创建另一个对象? - How to get information from an array of nested objects to create another one by comparing values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM