Consider the following array:
const options = [
{
uuid: '123312',
label: 'hello'
},
{
uuid: '523312',
label: 'there'
}
];
We want to transform it into this format:
{ result: { [uuid-label]: number } }
result: {
'123312-hello': 10 // placeholder random number
'523312-there': 20
}
Our current code snippet is as follows:
const randomIntFromInterval = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min);
const [result, setResult] = useState<Result>({} as Result);
useEffect(() => {
if(options.length) {
setResult(
options.map(o => {
return { [`${o.uuid}-${o.label}`]: randomIntFromInterval(0, 500) }
}))
}
}, [options]);
However, the above code produces an array like this:
[{'123312-hello': 10}, {'523312-there': 20}]
Here is a simplified version of the code for clarity:
const options = [{
uuid: '123312',
label: 'hello',
sortOrder: 0
},
{
uuid: '523312',
label: 'there',
sortOrder: 1
}
];
const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
const check = options.map(o => {
return {
[`${o.uuid}-${o.label}`]: randomIntFromInterval(0, 500)
}
});
console.log(check);
What adjustments are needed to achieve the desired outcome?