I have an application where i have 2 types of data:
- Persons
- 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.