const arrays = [{id: 1, name: "Superadmin"},{id: 2, name: "Admin"}] const convertArrayObjectToObject = ({ keys, array, parentKeys }) => { const convert = array.reduce((obj, item, index) => Object.assign(obj, { [`${keys}${index+1}`]: item }), {}); if(parentKeys) { return {[parentKeys]: { ...convert }} } return convert } console.log(convertArrayObjectToObject({ keys:'keberangkatan', array: arrays, parentKeys: 'keberangkatan' }))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter