0

i'm want the each items Array in objects state delete

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      storeData: [{
          "code": "f1",
          "name": "storage-no-1",
          "capacity": 125,
          "temperture": -18,
          "humidity": 3,
          "saveProducts": [{
              "code": "P1",
              "id": "1",
              "name": "product-no-1",
              "size": 20,
            },
            {
              "code": "P1",
              "id": "2",
              "name": "product-no-2",
              "size": 20,
            },
          ]
        },
        {
          "code": "f2",
          "name": "storage-no-2",
          "capacity": 15,
          "temperture": -18,
          "humidity": 25,
          "saveProducts": [{
              "code": "P1",
              "id": "1",
              "name": "product-no-1",
              "size": 20,
            },
            {
              "code": "P1",
              "id": "2",
              "name": "product-no-2",
              "size": 20,
            },
          ]
        },
      ]
    }
  }
}

my function code :

deleteItem = (saveProductsId,storeCode) => {
console.log("obj",saveProductsId)
console.log("ttt",storeCode);
this.setState(prevState => {
prevState.storeData.map(store => {
    if (store.code == storeCode) {
      return {
        ...store,
        saveProducts: [
            ...store.saveProducts.filter(product => product !==  saveProductsId)
        ]
      };
    } else { 
      return store;
    }
  })
})
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3
  • I've just realized that saveProducts is an array. You want to remove the items from each of saveProducts inside each storeData object? Commented Feb 2, 2019 at 14:43
  • yes i want this Commented Feb 2, 2019 at 14:46
  • I have updated the answer. If it does't work, I'll delete it Commented Feb 2, 2019 at 14:52

1 Answer 1

4

Go through each item in storeData and filter the saveProducts where the id !== saveProductsId:

this.setState(prevState => {
      const storeData = prevState.storeData.map(s => 
          ({ ...s, saveProducts: s.saveProducts.filter(p => p.id !== saveProductsId) }));
      return { storeData };
  })
Sign up to request clarification or add additional context in comments.

8 Comments

@A.RR Are there any objects in storeData which don't have a saveProducts property?
No, all objects have saveProducts property
I'm not sure if this is the issue but you're missing a comma after "humidity": 3 and "humidity": 25. Just before saveProducts declaration in state
I want to remove the selected item from the saveProducts
This answer is the correct one. It seems the question has been updated a few times and there are other bugs in it now. Like in your first setState example you're returning item which is undefined. Your second example doesn't follow the answer here because this answer has 2 array methods (map and filter) and you need both.
|

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.