0

Using the array of objects structure below, I need a means of scanning all object elements within this array, i.e. Monday through to Sunday and where the following criteria is met, reset that weeklyFlag to false, i.e.:

weeklyFlag is true 
weeklyStartTime is null
weeklyEndTime is null

then reset weeklyFlag to false

So using the above criteria for below example, anywhere where the above criteria is met, I need to reset that specific array object's weeklyFlag to false.

So this would mean for Monday, Tuesday's startEnd[1] index and Saturday.

let weeklyTimes = [
                    {
                        "day": "Monday",
                        "startEnd": [
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    },
                    {
                        "day": "Tuesday",
                        "startEnd": [
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": "2021-08-08T14:00:00.000Z",
                                "weeklyEndTime": "2021-08-08T15:00:00.000Z"
                            },
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    },
                    {
                        "day": "Wednesday",
                        "startEnd": [
                            {
                                "weeklyFlag": false,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    },
                    {
                        "day": "Thursday",
                        "startEnd": [
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": "2021-08-08T14:00:00.000Z",
                                "weeklyEndTime": "2021-08-08T15:00:00.000Z"
                            },
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": "2021-08-08T14:00:00.000Z",
                                "weeklyEndTime": "2021-08-08T15:00:00.000Z"
                            }
                        ]
                    },
                    {
                        "day": "Friday",
                        "startEnd": [
                            {
                                "weeklyFlag": false,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    },
                    {
                        "day": "Saturday",
                        "startEnd": [
                            {
                                "weeklyFlag": true,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    },
                    {
                        "day": "Sunday",
                        "startEnd": [
                            {
                                "weeklyFlag": false,
                                "weeklyStartTime": null,
                                "weeklyEndTime": null
                            }
                        ]
                    }
];

I was looking at nested for loops but unsure if array map or filter can be used?

2
  • filter would not be what you want and you would only use map if you wanted to return new array(s) Commented Aug 11, 2021 at 0:14
  • It depends a little on the structure of your application. If you want to modify the existing array's weeklyFlag then a for-loop would be the best choice, since you modify the existing array instead of creating a new one (e.g. using map) Commented Aug 11, 2021 at 0:19

3 Answers 3

2

Use a nested map to then check you condition and if met update the weeklyFlag.

weeklyTimes = weeklyTimes.map((day) => {
  day.startEnd = day.startEnd.map(
    ({ weeklyFlag, weeklyStartTime, weeklyEndTime }) => {
      if (weeklyFlag && !weeklyStartTime && !weeklyEndTime) weeklyFlag = false;
      return { weeklyFlag, weeklyStartTime, weeklyEndTime };
    }
  );
  return day;
});

Make sure to return the new element when mapping, and you can assign the new value to the original object as I did or make a new object.

Sign up to request clarification or add additional context in comments.

6 Comments

You just blew away the day field. I also think this over complicated. For loop is the simplest solution here.
I think i fixed it. And I don't think it was complicated just the syntax for maping can be kind of crazy.
no, you're just returning the unaltered day, you never assign the result of the inner map to anything. Also if you're using map() don't overwrite the original variable with the result.
You did it right inside the inner map, where all the fields are returned. But the fact that you're struggling with the code, and that it's so easy to make this mistake shows why it's not the best solution.
@seesharper would it be possible to get a simple for loop example that would cover my criteria?
|
2

It's a nested loop where we run through the objects, then run through the array contained by each. For each, we look at each "startEnd" array and modify if it meets the criterion.

function resetWeeklyFlag(wt) {
  const shouldBeReset = se => se.weeklyFlag && se.weeklyStartTime === null && se.weeklyEndTime === null;
  wt.startEnd.forEach(se => { // inner loop
    if (shouldBeReset(se)) se.weeklyFlag = false;
  })
}

let weeklyTimes = getWeeklyTimes();
weeklyTimes.forEach(wt => resetWeeklyFlag(wt)); // outer loop
console.log(weeklyTimes)

// return the OP data, just so we can see the answer code up front
function getWeeklyTimes() {
  return [{
      "day": "Monday",
      "startEnd": [{
        "weeklyFlag": true,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Tuesday",
      "startEnd": [{
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        },
        {
          "weeklyFlag": true,
          "weeklyStartTime": null,
          "weeklyEndTime": null
        }
      ]
    },
    {
      "day": "Wednesday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Thursday",
      "startEnd": [{
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        },
        {
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        }
      ]
    },
    {
      "day": "Friday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Saturday",
      "startEnd": [{
        "weeklyFlag": true,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Sunday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    }
  ];
}

Comments

1

I think your code is fine for what it's worth, but since you asked for the for loop approach I would use:

const weeklyTimes = getWeeklyTimes();
for (day of weeklyTimes) {
  for (s of day.startEnd) {
    if (s.weeklyFlag && !s.weeklyStartTime && !s.weeklyEndTime) {
      s.weeklyFlag = false;
    }
  }
}
console.log(JSON.stringify(weeklyTimes, null, 2));

function getWeeklyTimes() {
  return [{
      "day": "Monday",
      "startEnd": [{
        "weeklyFlag": true,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Tuesday",
      "startEnd": [{
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        },
        {
          "weeklyFlag": true,
          "weeklyStartTime": null,
          "weeklyEndTime": null
        }
      ]
    },
    {
      "day": "Wednesday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Thursday",
      "startEnd": [{
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        },
        {
          "weeklyFlag": true,
          "weeklyStartTime": "2021-08-08T14:00:00.000Z",
          "weeklyEndTime": "2021-08-08T15:00:00.000Z"
        }
      ]
    },
    {
      "day": "Friday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Saturday",
      "startEnd": [{
        "weeklyFlag": true,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    },
    {
      "day": "Sunday",
      "startEnd": [{
        "weeklyFlag": false,
        "weeklyStartTime": null,
        "weeklyEndTime": null
      }]
    }
  ];
}

Comments

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.