In Javascript, I'm trying to map an array of objects which has nested objects and the final result should be a new object with a key and the values joined together
For example, below the snippets
const res = [{
criteria: {
min: "25",
max: "100"
}
}, {
criteria: {
min: 0,
max: "85"
},
}, {
criteria: {
min: "10",
max: "85"
},
}, ].map(
(e) => ({
valid: e.criteria
})
)
console.log(res)
That map resulting in this way
[
{
"valid": {
"min": "25",
"max": "100"
}
},
{
"valid": {
"min": 0,
"max": "85"
}
},
{
"valid": {
"min": "10",
"max": "85"
}
}
]
My goal is the following result
[
{
"valid": ["25 - 100"]
},
{
"valid": ["0 - 85"]
},
{
"valid": ["10 - 85"]
}
]
I don't know how to make it happen and also criteria can come like criteria: null
The cases are as above we can have both min and max in one obj or have one object with only min or max and one obj with just null.
What could be the best way to achieve my goal that I don't know
UPDATE Some of the information changed as the data coming into this stage was modified to one clear.
The issue with previous coming data is that when min or max was 0 was removed and that causes issue further in the system.
As that has been correct by a colleague the cases are as follow
- criteria: min: 0 max: "10" it is valid and will result as ["0 - 10"]
- criteria: min: 1000 max: 0 will give an error so no present in data
What will be never in data min or max as alone values