0

I am trying to delete an item in a nested state but I have no idea how to implement that.

data structure

{
 property: {
  "_id" : "52",
  "name" : "random",
  "options" : ["item1", "item2", "item3"]
 }
}

to delete a property, I am just making a comparison like this and it is working

property: state.property.filter(data => data._id !== action.propertyId),

but the problem is when I want to delete an item of options array, how can I delete an item and compare the values

5
  • What's your expected output? I mean what result are you expecting? What do you want to do? Commented May 21, 2020 at 6:06
  • 1
    What you are trying to achieve here? You are using .filter which is method of array. And your property is object. .filter can only be used with array Commented May 21, 2020 at 6:06
  • @ArchitGarg i want to delete an item of array option, the scenario is when i click on a button, the item of the array must be deleted but in my current scenario, i must refresh the page to see the data change Commented May 21, 2020 at 6:11
  • @AbhinabRajopadhyaya the filter method is working as expected when i want to delete the property, and i am using it here just to illustrate my exemple Commented May 21, 2020 at 6:15
  • @Beloved You can do something like - state.property.data.filter(data => data !== action.dataId) Commented May 21, 2020 at 6:17

1 Answer 1

1

Maybe you're looking for below...

let state = {
  "property": {
    "_id": "52",
    "name": "random",
    "options": ["item1", "item2", "item3"]
  }
}

state.property.options = state.property.options.filter(ele => ele !== "item2")

console.log(state)

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

4 Comments

it is not working, it gives me the error TypeError: state.forEach is not a function
What is the type of state?
Updated answer again
the type of state is an object, the object ''property" that has 3 fields {_id, name and options which is an array} and i want to delete a single item of options, not the entire object property

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.