He estado luchando con este ejercicio.
addCharacter: function (name, lastName, house, dateOfBirth, isMuggle) { let id = 0; for (let i = 0; i < houses.length; i++) { if (house === houses[i]) { id = i + 1; } } if (id === 0) { return null; } const date = dateOfBirth.split("-").reverse().join("-"); const d = new Date(date); const year = d.getFullYear(); const character = { name, lastName, houseId: id, dateOfBirth, yearOfBirth: year, isMuggle, wand: {}, spells: [], }; characters.push(character); return character; } Necesito agregar propiedades al objeto wand con este formato {wood: wood, core: core, length: length}
addWand: function (name, wood, core, length) { const character = characters.find((character) => character.name === name); if (!character) return []; if (character.wand !== undefined) { return null } }He estado intentando con la notación de puntos, el método de asignación y el operador de distribución, pero no puedo hacerlo funcionar. Gracias por adelantado.
¿Es esto lo que estás buscando?
addWand: function (name, wood, core, length) { const character = characters.find((character) => character.name === name); if (!character) return []; if (character.wand == undefined) { // 'wand' property doesn't exist, so create it from wood, core, and length: character.wand = { wood, core, length } } else { // 'wand' property exists, so *add* wood, core and length: character.wand = { ...character.wand, wood, core,length } } } Si los personajes siempre se crean con una propiedad de wand (como parece suceder con su función addCharacter ), no es necesario verificar if (character.wand == undefined) { . En este caso, el código addWand se puede simplificar a:
addWand: function (name, wood, core, length) { const character = characters.find((character) => character.name === name); if (!character) return []; // 'wand' property exists, so *add* wood, core and length: character.wand = { ...character.wand, wood, core,length } }La asignación normal debería estar funcionando. Encuentre el fragmento a continuación:
let characters = []; let houses = ["big", "small", "medium"]; let addCharacter = (name, lastName, house, dateOfBirth, isMuggle) => { let id = 0; for (let i = 0; i < houses.length; i++) { if (house === houses[i]) { id = i + 1; } } if (id === 0) { return null; } const date = dateOfBirth.split("-").reverse().join("-"); const d = new Date(date); const year = d.getFullYear(); const character = { name, lastName, houseId: id, dateOfBirth, yearOfBirth: year, isMuggle, wand: {}, spells: [], }; characters.push(character); return character; } let addWand = function(name, wood, core, length) { const character = characters.find((character) => character.name === name); if (!character) return []; if (character.wand === undefined) { return null } else { character.wand = { "wood": wood, "core": core, "length": length }; } } addCharacter("john", "doe", "big", "18/9/1994", true); addWand("john", "123", "12", "12"); addCharacter("misty", "doe", "big", "18/9/1994", true); addWand("misty", "1234", "123", "123"); console.log(characters);