0

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

1 Answer 1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for putting me in the right direction.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.