Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
¿Cómo agregar una propiedad a un objeto dentro de otra función?

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.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

¿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 } }
about 4 years ago · Juan Pablo Isaza Denunciar

0

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);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda