简体   繁体   中英

How to add property to object inside another function?

I've been struggling with this exercise.

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

I need to add properties to wand object with this format {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
    }
}

Been trying with dot notation, assign method and spread operator but I can't make it work. Thanks in advance.

Is this what you're looking for?

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 }
    }
}

If characters are always created with a wand property (as seems to happen with your addCharacter function), there is no need to check for if (character.wand == undefined) { . In this case the addWand code can be simplified to:

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 }
}

Normal Assignment should be working. Find the snippet below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM