Hello there, I am looking to connect a dropdown menu with values from an object array. Here is what I have tried so far and need guidance on how to proceed:
JavaScript Code:
const objArray = [
{
"type": "select",
"required": false,
"label": "WorkTime",
"name": "select-1659012135405-0",
"customFieldId": "4330",
"fieldCode": "WT1",
"advancedSettings": "Edit",
"typeDescription": "Select",
"values": [
{
"label": "Apples",
"value": "AP",
"selected": false
},
{
"label": "Bananas",
"value": "BA",
"selected": false
},
{
"label": "Pineapples",
"value": "PI",
"selected": false
}
]
},
{
"type": "select",
"required": false,
"label": "WorkHours",
"name": "select-1659012135415-0",
"customFieldId": "13916",
"fieldCode": "WH",
"advancedSettings": "Edit",
"typeDescription": "Select",
"values": [
{
"label": "Day",
"value": "D",
"selected": false
},
{
"label": "Night",
"value": "N",
"selected": false
}
]
}
];
const result = objArray.map(({customFieldId, fieldCode, label, values}) => {
if (!values) { return; }
return values.reduce((obj, key, index) => {
return Object.assign(obj, {
[`${customFieldId}-${index}`]:
{
value: `${customFieldId}-${fieldCode}-${key.value}`,
text: `${fieldCode} - ${label} - ${key.label}`
}
});
}, {});
});
console.log(result);
Current Result:
[{
4330-0: {
text: "WT1 - WorkTime - Apples",
value: "4330-WT1-AP"
},
4330-1: {
text: "WT1 - WorkTime - Bananas",
value: "4330-WT1-BA"
},
4330-2: {
text: "WT1 - WorkTime - Pineapples",
value: "4330-WT1-PI"
}
}, {
13916-0: {
text: "WH - WorkHours - Day",
value: "13916-WH-D"
},
13916-1: {
text: "WH - WorkHours - Night",
value: "13916-WH-N"
}
}]
Expected Result:
[
{
text: "WT1 - WorkTime - Apples",
value: "4330-WT1-AP"
},
{
text: "WT1 - WorkTime - Bananas",
value: "4330-WT1-BA"
},
{
text: "WT1 - WorkTime - Pineapples",
value: "4330-WT1-PI"
},
{
text: "WH - WorkHours - Day",
value: "13916-WH-D"
},
{
text: "WH - WorkHours - Night",
value: "13916-WH-N"
}
]
I want to set up the dropdown to display "text" as the DisplayText and "value" as the Key. How can I achieve this?