Tengo este objeto generado dinámicamente cuyas propiedades se muestran undefined . Parece que no entiendo por qué sucedió.
Ayuda por favor
aquí está el código.
let submittionObject = {}; let submit = {}; let theSubmission = ['english', 'mathematics', 'r1Submitted', 'biology']; const theSubmit = theSubmission.map((item, index) => { if(item.indexOf('Submitted') !== -1){ console.log(item.indexOf('Submitted')) submit = {...submittionObject, [item]: false}; }else{ submit = {...submittionObject, [`r${index+1}Submitted`]: true}; } return submit; }); const {r1Submitted, r2Submitted, r3Submitted, r4Submitted} = theSubmit; El valor del objeto desestructurado debe ser un valor booleano de true o false , pero en su lugar se muestra undefined
Parece que estás buscando algo como esto :
const theSubmission = ['english', 'mathematics', 'r1Submitted', 'biology']; const submittionObject = {}; theSubmission.forEach((subject, index) => { submittionObject[theSubmission[index]] = (subject.indexOf('Submitted') !== -1) ? true : false; }); console.log(submittionObject);map devuelve una matriz y la propiedad 'r1Submitted' y así sucesivamente no existe en una matriz. Deberá recorrer la matriz para encontrar el elemento con esa propiedad.
const r1Submitted = theSubmit.find(i => i.r1Submitted)
Devuelve un valor booleano en la devolución de llamada. Prueba esto,
let submittionObject = {}; let submit = {}; let theSubmission = ['english', 'mathematics', 'r1Submitted', 'biology']; const theSubmit = theSubmission.map((item, index) => { if(item.indexOf('Submitted') !== -1){ console.log(item.indexOf('Submitted')) item = false; }else{ item = true; } return item; });