I am seeking help in developing a function that takes an object with a boolean attribute and changes its value to false, then proceeds to the next item in an array and changes its value to true. If the provided object is the last item in the array, the function should cycle back to the first item.
To clarify, these are the desired outcomes when the function is called:
Example 1
const arr = [
{
id: 1,
chosen: false
},
{
id: 2,
chosen: true
},
{
id: 3,
chosen: false
},
];
const chosenObject = arr[1];
functionToBeCreated(chosenObject); // Expected Outcome: [{ id: 1, chosen: false }, { id: 2, chosen: false }, { id: 3, chosen: true }]
Example 2
const arr = [
{
id: 1,
chosen: false
},
{
id: 2,
chosen: false
},
{
id: 3,
chosen: true
},
];
const chosenObject = arr[2];
functionToBeCreated(chosenObject); // Expected Outcome: [{ id: 1, chosen: true }, { id: 2, chosen: false }, { id: 3, chosen: false }]
Any suggestions on how this functionality can be achieved?