0

I am trying to filter through an array of objects and delete one of the object but keep the rest inside an array of objects but i keep returning either an array of arrays or i create nested objects. Is it possible to send in an array of objects and return and array of objects without that specific object? Below is the code I have been trying to work with.

      function deleteWorkout(workoutName) {
    
    const updatedArray = myWorkoutToDisplay.map((item) => item.newWorkToAdd.filter((workout) => workout.name !== workoutName))
    const objectArray = [{updatedArray}]
    const newWorkToAdd = objectArray.filter(e => e.length)
    const workouts = [{newWorkToAdd}]
    setMyWorkoutToDisplay(updatedArray)
  }
6
  • yo can you indent that code and provide more minimal code? i don't know what myWorkoutToDisplay is or newWorkToAdd is for certain. Commented Jul 25, 2021 at 2:52
  • This might be helpful. youtube.com/watch?v=zdp0zrpKzIE&ab_channel=AkshaySaini Commented Jul 25, 2021 at 2:56
  • Sorry its a bit confusing because i tried so many different ways. workoutName is an array of objects i sent through the function and I'm trying to compare that using filter on myWorkoutDisplay with is also an array of objects but it has a key newWorkToAdd that i need to map first to get to the objects. It's an array then my newWorkToAdd then an array of objects. Commented Jul 25, 2021 at 3:08
  • You need to provide an example of your array of Objects and and the value to get a better answer we can't guess how your array looks Commented Jul 25, 2021 at 9:53
  • "workoutToAdd": [ { "name": "1\nKettlebell Slingshot (Kettlebell Around the World)", "image": "https://kettlebellsworkouts.com/wp-content/uploads/2018/08/01_kettlebell-slingshot.png" }, { "name": "2\nKettlebell Halo", "image": "https://kettlebellsworkouts.com/wp-content/uploads/2018/08/02_kettlebell-halo.png" }] Commented Jul 25, 2021 at 12:49

1 Answer 1

1

You can easily do this with Array.prototype.filter. I guess the easiest way to delete 1 object is like this:

//let arr = arrayLike
//let objToDelete = whatever you want to delete
let newArr = arr.filter(obj => obj !== objToDelete)

newArr now has the array, without the deleted item. arr however still has it. To delete item by index, use this:

//let arr = arrayLike
//let ind = index to delete
let newArr = arr.filter((_, index) => index !== ind)
Sign up to request clarification or add additional context in comments.

7 Comments

I tried doing this but it didn't work. Im not sure if its because i had to map it first then use this.
What do you mean by "it didn’t work"? Please clarify what happened instead of the expected result
It gave me back an array of arrays instead of an array of objects.
Can you give me what you are trying to filter so I can test it myself?
I copy and pasted it from the consol - newWorkToAdd: Array(1) 0: image: "kettlebellsworkouts.com/wp-content/uploads/2018/08/…" name: "2\nKettlebell Halo"
|

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.