Untitled
unknown
javascript
a year ago
3.4 kB
3
Indexable
const schemaArr = { "body": [ { "id": "updateKycStatus", "label": "Kyc Status Update", "type": "select", "defaultVisible": true, "relative": false, "options": { "APPROVED": { "id": "APPROVED", "label": "Approve", "preSelect": false, "defaultVisible": true }, "REJECTED": { "id": "REJECTED", "label": "Reject", "preSelect": false, "defaultVisible": false, "dependentFilters": [ "rejectReason" ] } }, "required": false }, { "id": "rejectReason", "label": "Enter Reason for Reject", "type": "textBox", "defaultVisible": false, "relative": false, "required": false }, { "id": "updateTRM", "label": "TRM Status Update", "type": "select", "defaultVisible": true, "relative": false, "options": { "APPROVED": { "id": "APPROVED", "label": "Approve", "preSelect": false, "defaultVisible": true }, "REJECTED": { "id": "REJECTED", "label": "Reject", "preSelect": false, "defaultVisible": false, "dependentFilters": [ "rejectReason" ] } }, "required": false } ] }; const state = { "updateKycStatus": { "value": [ "a0" ] }, "updateTRM": { "value": [ "a1" ] }, "rejectReason": "", "rejectReason2": { "value": "test comnt" } }; const newConvertedState = {}; // Iterate through the keys in the state object for (const key in state) { if (state.hasOwnProperty(key)) { // Find the corresponding schema item in schemaArr const schemaItem = schemaArr.body.find(item => item.id === key); // If no schema item is found for the current key if (!schemaItem) { console.warn(`No id found in schemaArr for the key ${key}`); // Set the value to null in newConvertedState newConvertedState[key] = null; } else { // If the schema item type is "select" if (schemaItem.type === "select") { // If the value is not empty if (state[key] && state[key].value && state[key].value.length > 0) { const index = parseInt(state[key].value[0].substr(1)); const optionsKeys = Object.keys(schemaItem.options); // If the index is within bounds of the available options if (index >= 0 && index < optionsKeys.length) { const selectedOption = schemaItem.options[optionsKeys[index]]; newConvertedState[key] = selectedOption.id; } else { console.warn(`Index out of bounds for the key ${key}`); // Set the value to null in newConvertedState newConvertedState[key] = null; } } else { // Set the value to null in newConvertedState newConvertedState[key] = null; } } else if (schemaItem.type === "textBox") { // For "textBox" type, if the value is empty, set it to null; otherwise, keep it as is newConvertedState[key] = state[key] && state[key].value ? state[key].value : null; } else { // For other types, set the value to null newConvertedState[key] = null; } } } } console.log(newConvertedState);
Editor is loading...