Home > Mobile >  How to add property to object inside another function?
How to add property to object inside another function?

Time:03-05

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related