-1

I'm returning key-value fields from the database, how do merge the two values into a new key-value pair?

{
  "conditions": [{
        "key": "target_condition",
        "value": "100"
    },
    {
        "key": "target_count_or_value",
        "value": "value"
    }
  ]
}

How to:

{
"conditions": {
    "target_condition": "110",
    "target_count_or_value": "quantity" }
}
  
htatd
  • 11
  • 2

1 Answers1

0
data.conditions = data.conditions.map(item=>{[item.key]: item.value})

this uses the map function and dynamic object keys to build new objects

schloemi
  • 41
  • 1
  • 5
  • Thanks for putting me in the right direction. – htatd Aug 01 '22 at 09:19
  • 1
    This will throw an error as the `{}` is treated as a code block in `item => {}`, not an object. OP also wants an object to be set for `conditions`, and not an array which is what you're currently trying to set `conditions` to. – Nick Parsons Aug 01 '22 at 09:20