Home > database >  how to replace partial content of JS object array
how to replace partial content of JS object array

Time:12-29

I have a JS object array as :

[
  { workoutName: 'Farmers Walk' },
  { workoutName: 'BW Lateral Lunge Hop (Left)' },
  { workoutName: 'Dumbbell Cross Chops (Right)' },
  { workoutName: 'BW Oblique Jumping Jacks' },
  { workoutName: 'BW Wide Press Parallel' },
  { workoutName: 'BW Single Leg Lunge Jump (Left)' },
  { workoutName: 'BW Jump Squat' },
  { workoutName: 'BW Squat to Toe Tap' }
]

I'm saving the objects that are (Left) & (Right) to a new array.

let substrL = 'Left'
let substrR = 'Right'
let remaining = [] 
  results.find((o) => {
     if (o.workoutName.includes (substrR) || o.workoutName.includes (substrL))
     //results.map(obj => ({ workoutName: obj.workoutName.replace('Left','Right')}))
          remaining.push(o)
  
  });

What I cannot do is, I'm trying to replace the partial string of the remaining[] array.

in this example , the results are is

[
  { workoutName: 'BW Lateral Lunge Hop (Left)' },
  { workoutName: 'Dumbbell Cross Chops (Right)' },
  { workoutName: 'BW Single Leg Lunge Jump (Left)' }
]

what I would like to do / the expected outcome is to have the opposite of the results arr.

[
   { workoutName: 'BW Lateral Lunge Hop (Left)' }, //workoutName: 'BW Lateral Lunge Hop (Right)'
   { workoutName: 'Dumbbell Cross Chops (Right)' },//workoutName: 'Dumbbell Cross Chops (Left)'
   { workoutName: 'BW Single Leg Lunge Jump (Left)' }//workoutName: 'BW Single Leg Lunge Jump (Right)'
]

CodePudding user response:

You did everything and just needed the opposite of what is present. Without affecting the original object, you can use the spread operator to store the object, then check what type of content is stored within the parentheses, and alter them:

var results = [
  { workoutName: 'Farmers Walk' },
  { workoutName: 'BW Lateral Lunge Hop (Left)' },
  { workoutName: 'Dumbbell Cross Chops (Right)' },
  { workoutName: 'BW Oblique Jumping Jacks' },
  { workoutName: 'BW Wide Press Parallel' },
  { workoutName: 'BW Single Leg Lunge Jump (Left)' },
  { workoutName: 'BW Jump Squat' },
  { workoutName: 'BW Squat to Toe Tap' }
]

let substrL = 'Left'
let substrR = 'Right'
let remaining = []
results.find((o) => {
  if (o.workoutName.includes(substrR) || o.workoutName.includes(substrL)) {
    let data = { ...o }
    if (o.workoutName.includes(substrR))
      data.workoutName = o.workoutName.substring(0, o.workoutName.indexOf(substrR))   substrL   ")"
    else
      data.workoutName = o.workoutName.substring(0, o.workoutName.indexOf(substrL))   substrR   ")"
    remaining.push(data)
  }
});

console.log(remaining)

CodePudding user response:

I'd think about this in terms of a map on the original array, producing an array of the original objects with new props.

const array = [
  { workoutName: 'Farmers Walk' },
  { workoutName: 'BW Lateral Lunge Hop (Left)' },
  { workoutName: 'Dumbbell Cross Chops (Right)' },
  { workoutName: 'BW Oblique Jumping Jacks' },
  { workoutName: 'BW Wide Press Parallel' },
  { workoutName: 'BW Single Leg Lunge Jump (Left)' },
  { workoutName: 'BW Jump Squat' },
  { workoutName: 'BW Squat to Toe Tap' }
];

const directionWords = [ "Left", "Right" ];
const complements = { Left: "Right", Right: "Left" };
const augmentedArray = array.map(obj => {
  const directionWord = directionWords.find(word => obj.workoutName.includes(word));
  if (directionWord) {
    const complement = complements[directionWord];
    const complementName = obj.workoutName.replace(directionWord, complement);
    return { ...obj, directionWord, complement, complementName }
  }
  return obj;
})

console.log(augmentedArray)

Any result we might want can now be extracted. For example, the OP's "remainder" objects, with updated names...

const array = [
  { workoutName: 'Farmers Walk' },
  { workoutName: 'BW Lateral Lunge Hop (Left)' },
  { workoutName: 'Dumbbell Cross Chops (Right)' },
  { workoutName: 'BW Oblique Jumping Jacks' },
  { workoutName: 'BW Wide Press Parallel' },
  { workoutName: 'BW Single Leg Lunge Jump (Left)' },
  { workoutName: 'BW Jump Squat' },
  { workoutName: 'BW Squat to Toe Tap' }
];

const directionWords = [ "Left", "Right" ];
const complements = { Left: "Right", Right: "Left" };
const augmentedArray = array.map(obj => {
  const directionWord = directionWords.find(word => obj.workoutName.includes(word));
  if (directionWord) {
    const complement = complements[directionWord];
    const complementName = obj.workoutName.replace(directionWord, complement);
    return { ...obj, directionWord, complement, complementName }
  }
  return obj;
})

const updatedRemainders = augmentedArray
  .filter(obj => obj.complementName)
  .map(obj => ({ workoutName: obj.complementName }));
console.log(updatedRemainders)

  • Related