Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

javascript - Add data in a specific proprety of object

I have this application: https://codesandbox.io/s/misty-smoke-n5f1e?file=/Person.js:123-1128
The idea of the application is next:

  1. Near each person is a select tag with their genre.
  2. Every person should be able to assign it self to the specific toy, selecting an option. ex: Carl can assign his self only to toyType: "M", by selecting M from select, and after selecting i should get the next data:
    0: Object
    color: "red"
    toyType: "M"
    persons: Array[1]
              0: Object
              name: "Carl"
              age: 17
    1: Object
    color: "white"
    toyType: "F"
  1. If another user clicks on M also his name and age should be added in persons array from above.
  2. Disabled are options that not coresponds with the type proprety of person; EX: Lisa has M dissabled bicause her type is not M but F;
  3. Each person can add it self to a specific toy just one time not twice.
    const Person = ({ person, k }) => {
      // console.log(person)
      const thisPerson = { name: person.name, age: person.age };
      function handleChange(value) {
        console.log(`selected ${value}`);
        const result = toys.map((i) => {
          if (value === i.toyType) {
            return {
              ...i,
              persons: toys.persons ? [...i.persons, thisPerson] : [thisPerson]
            };
          } else {
            return { ...i };
          }
        });
        console.log("result", result);
        return result;
      }
      return (
        <div>
          <Select
            placeholder="select"
            style={{ width: 120 }}
            onChange={handleChange}
          >
            {toys.map((i, k) => (
              <Option
                disabled={
                  i.toyType.toLocaleLowerCase() !== person.type.toLocaleLowerCase()
                }
                key={k}
                value={i.toyType}
              >
                {i.toyType}
              </Option>
            ))}
          </Select>
          <span>{person.name}</span>
        </div>
      );
    };
    export default Person;

Here,


    if (value === i.toyType) {
            return {
              ...i,
              persons: toys.persons ? [...i.persons, thisPerson] : [thisPerson]
            };
          } else {
            return { ...i };
          }

...i try to implement the functionality where users can assign their self to a specific toy, but appaers an issue when another user wants to add his self to one toy where exist already a person, because the previous person dissapears and insted of him appears the last, but i need to have an array of persons.
Also i cant figure out how to prohibit users to asssign their selfs twice to the same toy. Question: Who knows how to solve the issues? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...