Home > Mobile >  repeat objects inside the array n-times after certain index
repeat objects inside the array n-times after certain index

Time:07-21

enter image description here

when the similar circuit_id ends and the sets_rounds is greater than 1 e.g. 2 in our case. I want them to repeat after the last similar circuit_id.

from position 4th to 9th they have the same circuit_id and the rounds are 2 i want them to repeat once from position 10th.

here is the array which i want to manipulate,

this is what i have

[
{
    "circuit_id": "226",
    "sets_rounds": 1,
    "video_name": "Jumping Jack",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/185/video/original/nog_105.mp4"
},
{
    "circuit_id": "227",
    "sets_rounds": 1,
    "video_name": "Thruster",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/210/video/original/nog_131.mp4"
},
{
    "circuit_id": "228",
    "sets_rounds": 1,
    "video_name": "Shuffle",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/109/video/original/nog_015.mp4"
},
{
    "circuit_id": "230",
    "sets_rounds": 1,
    "video_name": "Side Lunge",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/174/video/original/nog_091.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Speed Skaters",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/121/video/original/nog_028.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up right",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2072/video/original/nog_263.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Squat jacks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2065/video/original/nog_256.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up left",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2073/video/original/nog_264.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Broad Jumps",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/108/video/original/nog_014.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Cross Mountain Jack",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/209/video/original/nog_130.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Squat Knee Raise Twist",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/153/video/original/nog_063.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Reverse Marching Planks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2243/video/original/nog_311.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knees to stand up",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2060/video/original/nog_249.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Dish Bicycles",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2253/video/original/nog_321.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Triples (lunge - lunge - squat)",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1954/video/original/nog_236.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knee Plank Side Touch",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/128/video/original/nog_036.mp4"
},
{
    "circuit_id": "214",
    "sets_rounds": 1,
    "video_name": "Quadricep Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1031/video/original/stc_001.mp4"
},
{
    "circuit_id": "215",
    "sets_rounds": 1,
    "video_name": "Seated Shoulder Stretch",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1050/video/original/stc_020.mp4"
},
{
    "circuit_id": "222",
    "sets_rounds": 1,
    "video_name": "Standing Calf Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1033/video/original/stc_003.mp4"
},
{
    "circuit_id": "225",
    "sets_rounds": 1,
    "video_name": "Lower Back Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1052/video/original/stc_022.mp4"
}]

this is what I want

[
{
    "circuit_id": "226",
    "sets_rounds": 1,
    "video_name": "Jumping Jack",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/185/video/original/nog_105.mp4"
},
{
    "circuit_id": "227",
    "sets_rounds": 1,
    "video_name": "Thruster",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/210/video/original/nog_131.mp4"
},
{
    "circuit_id": "228",
    "sets_rounds": 1,
    "video_name": "Shuffle",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/109/video/original/nog_015.mp4"
},
{
    "circuit_id": "230",
    "sets_rounds": 1,
    "video_name": "Side Lunge",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/174/video/original/nog_091.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Speed Skaters",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/121/video/original/nog_028.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up right",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2072/video/original/nog_263.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Squat jacks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2065/video/original/nog_256.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up left",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2073/video/original/nog_264.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Broad Jumps",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/108/video/original/nog_014.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Cross Mountain Jack",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/209/video/original/nog_130.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Speed Skaters",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/121/video/original/nog_028.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up right",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2072/video/original/nog_263.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Squat jacks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2065/video/original/nog_256.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up left",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2073/video/original/nog_264.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Broad Jumps",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/108/video/original/nog_014.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Cross Mountain Jack",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/209/video/original/nog_130.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Squat Knee Raise Twist",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/153/video/original/nog_063.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Reverse Marching Planks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2243/video/original/nog_311.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knees to stand up",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2060/video/original/nog_249.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Dish Bicycles",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2253/video/original/nog_321.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Triples (lunge - lunge - squat)",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1954/video/original/nog_236.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knee Plank Side Touch",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/128/video/original/nog_036.mp4"
},
{
    "circuit_id": "214",
    "sets_rounds": 1,
    "video_name": "Quadricep Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1031/video/original/stc_001.mp4"
},
{
    "circuit_id": "215",
    "sets_rounds": 1,
    "video_name": "Seated Shoulder Stretch",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1050/video/original/stc_020.mp4"
},
{
    "circuit_id": "222",
    "sets_rounds": 1,
    "video_name": "Standing Calf Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1033/video/original/stc_003.mp4"
},
{
    "circuit_id": "225",
    "sets_rounds": 1,
    "video_name": "Lower Back Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1052/video/original/stc_022.mp4"
}]

CodePudding user response:

You could use reduce with a couple of state variables to do this:

const remapData = () => {
  let circuitId;
  let itemsToCopy = [];

  return data.reduce((memo, item) => {
    // Circuit id has changed, push all the current items to copy   
    if (circuitId !== item.circuit_id) {
      memo.push(...itemsToCopy);
      itemsToCopy = [];
    }

    memo.push(item);

    circuitId = item.circuit_id;
    if (item.sets_rounds === 2) {
      itemsToCopy.push(item);
    }

    return memo;
  }, []);  
}

Here's a codesandbox demo

CodePudding user response:

I would do this, creating an output array and fill it using a for-loop for every set_rounds.

const output = [];

for(let i=0; i<input.length; i  )
{
  let times = input[i].sets_rounds;
  for (let j = 0; j < times; j  ) output.push(input[i]);
}

const input = [
{
    "circuit_id": "226",
    "sets_rounds": 1,
    "video_name": "Jumping Jack",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/185/video/original/nog_105.mp4"
},
{
    "circuit_id": "227",
    "sets_rounds": 1,
    "video_name": "Thruster",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/210/video/original/nog_131.mp4"
},
{
    "circuit_id": "228",
    "sets_rounds": 1,
    "video_name": "Shuffle",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/109/video/original/nog_015.mp4"
},
{
    "circuit_id": "230",
    "sets_rounds": 1,
    "video_name": "Side Lunge",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/174/video/original/nog_091.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Speed Skaters",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/121/video/original/nog_028.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up right",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2072/video/original/nog_263.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Squat jacks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2065/video/original/nog_256.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Donkey kick up left",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2073/video/original/nog_264.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Broad Jumps",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/108/video/original/nog_014.mp4"
},
{
    "circuit_id": "231",
    "sets_rounds": 2,
    "video_name": "Cross Mountain Jack",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/209/video/original/nog_130.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Squat Knee Raise Twist",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/153/video/original/nog_063.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Reverse Marching Planks",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2243/video/original/nog_311.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knees to stand up",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2060/video/original/nog_249.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Dish Bicycles",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/2253/video/original/nog_321.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Triples (lunge - lunge - squat)",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1954/video/original/nog_236.mp4"
},
{
    "circuit_id": "229",
    "sets_rounds": 1,
    "video_name": "Knee Plank Side Touch",
    "rest_sets": 15,
    "rest_exercise": 15,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/128/video/original/nog_036.mp4"
},
{
    "circuit_id": "214",
    "sets_rounds": 1,
    "video_name": "Quadricep Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1031/video/original/stc_001.mp4"
},
{
    "circuit_id": "215",
    "sets_rounds": 1,
    "video_name": "Seated Shoulder Stretch",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1050/video/original/stc_020.mp4"
},
{
    "circuit_id": "222",
    "sets_rounds": 1,
    "video_name": "Standing Calf Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1033/video/original/stc_003.mp4"
},
{
    "circuit_id": "225",
    "sets_rounds": 1,
    "video_name": "Lower Back Stretch Alternating",
    "rest_sets": 0,
    "rest_exercise": 0,
    "video_url": "https://dcx828kjwpmk.cloudfront.net/switch-solutions/exercises/1052/video/original/stc_022.mp4"
}];

const output = [];

for(let i=0; i<input.length; i  )
{
  let times = input[i].sets_rounds;
  for (let j = 0; j < times; j  ) output.push(input[i]);
}

console.log(input);
console.log(output);

  • Related