JSFiddle - React, Tailwind, and code Playground

by Gabriel Correa

JavaScript

const obj = { 
  "Alkali metals": "Rjiwsjajmal", 
  "Non metals": "Carbon,Oxygen", 
  "Metals": "Sodium", 
  "Others non metals": "Fjwaiaa", 
  "Actinoid elements": "532189" 
};

const arr = [
  { 
    "_id": "6368fbd96bc236fe074c0826", 
    "createdAt": "2022-11-07 09:36:40", 
    "author": "adicionarei posteriormente", 
    "autoFill": true, 
    "checkBox": {}, 
    "dica": "Element name", 
    "field": "Alkali metals", 
    "fieldtype": "Text", 
    "index": 0, 
    "marker": false, 
    "sufixo": null 
  }, 
  { 
    "_id": "633ac872e78fa7ebee03b8bf", 
    "createdAt": "2022-10-03 08:33:06", 
    "author": "adicionarei posteriormente", 
    "checkBox": { 
      "0": "Carbon", 
      "1": "Nitrogen", 
      "2": "Oxygen" 
    }, 
    "dica": "Element name", 
    "field": "Non metals", 
    "fieldtype": "multipleSelection", 
    "index": 2, 
    "sufixo": null, 
    "autoFill": true, 
    "marker": true 
  }
];

const result = arr.reduce((acc, curr) => {
  if (curr.fieldtype === "multipleSelection") {
    const key = curr.field;
    acc[key] = acc[key].split(",").map(element => element);
    return acc;
  }
  return acc;
}, obj);

console.log(result);