1

I have a form, that is filled in for 18 holes on a golf course, each of the holes has the following fields, hole, par, stroke, yards, courseid

As each input field is filled in, it fires onChange and sets the value of a variable in form state, to the input fields value.

In my form object I have all these fields,

const [form, setForm] = useState([
    {
        hole1: 1,
        par1: '',
        stroke1: '',
        yards1: ''
    },
    {
        hole2: 2,
        par2: '',
        stroke2: '',
        yards2: ''
    },
    {
        hole3: 3,
        par3: '',
        stroke3: '',
        yards3: ''
    },

   //repeated for 18 holes

When completed and submit is clicked, it fires the save function, A list is sent to the backend (SpringBoot) to be saved in dB.

As there are 18 holes, I need to loop over the data so that I can fill in 18 objects to put into the list to send to the backend.

I have made a holes object

   let[holes, setHoles] = useState({
        "hole": '',
        "par": '',
        "stroke": '',
        "yards": '',
        "course": {
            "courseid": ''
        }
    });
        

which I now want to populate with the values from the form data.

so I want to set the holes values for each field to that for,

form.hole1,
form.par1,
form.stroke1,
form.yards1

Then add that holes object to the list, then run the loop again and add all the values for hole 2, etc etc until all 18 holes are done.

When using a loop like,

for (let i= 1; i< 19; i++) {
            holes = {
                "hole": index,
                "par": form.par,
                "stroke": form.stroke,
                "yards": form.yards,
                "course": {
                    "courseid": 3
                }
            }
            const newList = list.concat({holes})
            list = newList;
        };

how is it best to tell it to take form.par1 on the first loop, then form.par2 on the second loop etc.

I feel like I need two loops running here, so that it starts off looping through all the numbers 1-18, and before moving to the next number it loops through the objects in the form,

so it starts at hole 1, get the holes object, gets the value from the first form object, sets the 4 fields in holeto those in the firstform object i.e par1, yards1 etc, then concats the hole object to the list then moves on to the number 2 and continues this till all 18 are complete, but I am not sure how I can achieve this.

2
  • The design/explanation here is a bit confusing, but I think what you are looking for is string interpolation in your object properties. You can do "par": form[`par${i}`], to construct the property name that you need. Commented Oct 31, 2021 at 22:28
  • Great thanks, it took me ages to write that question out as it's hard to explain what I wanted. I tried loads of ways of appending the value of i in the loop to par, but couldn't get it to work. I will try this tomorrow Commented Oct 31, 2021 at 22:51

1 Answer 1

2

Given the form array shaped as you've described, you can map this to the desired list of holes like so:

const list = form.map((hole, i) => {
  const num = i + 1;
  return {
    hole: num,
    par: hole[`par${num}`],
    stroke: hole[`stroke${num}`],
    yards: hole[`yards${num}`],
    course: {
      courseid: 3,
    },
  };
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This works great. But has now shown another issue, but I will open a new question for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.