Add data inside an object depending by a condition

Viewed 68

I have an application where i have 2 types of data:

  1. Persons
  2. Toys
    Conditions:
  • User can assign a person to one of the toys, depending of: if the toyType === type of the person. For example,

  {
    name: "Lisa",
    age: 7,
    type: "F"
  },

..can't be assigned to a toy with toyType M, only F

  • User can assign a person to just one type of toys.
    My code:

 const info = [
  {
    name: "Bill",
    age: 11,
    type: "M"
  },
  {
    name: "Lisa",
    age: 7,
    type: "F"
  },
  {
    name: "Carl",
    age: 17,
    type: "M"
  },
  {
    name: "John",
    age: 8,
    type: "M"
  }
];
 const toys = [
  {
    color: "red",
    toyType: "M"
  },
  {
    color: "white",
    toyType: "F"
  }
];

const thisPerson = { name: "Carl",age: 17};

function app(selectedtoyType) {
  const result = toys.map((i) => {
    if (selectedtoyType === i.toyType) {
      return {
        ...i,
        persons: toys.persons ? [...i.persons, thisPerson] : [thisPerson]
      };
    } else {
      return { ...i };
    }
  });
  return result;
}
console.log('1 time  assign', app('M'))
console.log('2 time  assign', app('M'))

I took an example with: const thisPerson = { name: "Carl",age: 17};, and I tried to assign him 2 times.
Here I should get just one person in persons array, the next time I should get an error in the console, also I should be able to add another person with type M to this array, of course a different one than the previous.
Question: What is the problem with my code?
ps: the code is just a simuation of the app.

2 Answers

Maybe you want something like this

const toys = [ { color: "red", toyType: "M", }, { color: "white", toyType: "F", }, ];
let ps = {};
const person = { name: "Carl", age: 17 };

function app(person, selectedtoyType) {
  if (ps[person.name]) console.log("name alreasy used");
  else {
    toys.forEach((t) => {
      if (selectedtoyType.toUpperCase() == t.toyType && !ps[person.name]) {
        ps[person.name] = { ...person, ...t };
      }
    });
    return Object.values(ps);
  }
}
console.log("1 time  assign", app(person, "M"));
console.log("2 time  assign", app(person, "M"));

You must check if the person is already in the dictionary, and I also recommend you to change the const result to this.result, so you can access it publicly:

const toys = [
  {
    color: "red",
    toyType: "M"
  },
  {
    color: "white",
    toyType: "F"
  }
];

var thisPerson = { name: "Carl",age: 17};

function app(selectedtoyType) {
  var assignedTwice
  this.result = this.result || toys;
  this.result = this.result.map((i) => {
    if (selectedtoyType === i.toyType) {
      if(i.persons) {
          assignedTwice = false;
          //if person exists, throw error
          i.persons.forEach(person => {
              if(person == thisPerson) {
                  assignedTwice = true;
              }
          });
          var persons = [...i.persons, thisPerson];
      } else {
          var persons = [thisPerson];
      }
      if(assignedTwice) return i;
      
      return {
        ...i,
        persons: persons
      };
    } else {
      return i;
    }
  });
  if(assignedTwice) return "Person assigned twice";
  
  return this.result;
}

//assign toys to new toys
console.log('1 time assign', app('M'));
console.log('2 time  assign', app('M'));
thisPerson = { name: "Carlos",age: 17};
console.log('3 time assign', app('M'));

This snippet now uses static variables as intended in the question. It updates only the inner dictionary app.result and not the toys dictionary. When a person is assigned twice, it exits the function and returns a message.

Related