0

I want to filter only the fields element to delete it. I already manage to delete the all of the data but I want the option to delete also only one value of the fields. I manage to filter the data by this piece of code

this.setState(
  state => ({
    data: state.data.filter((row, j) => j !== i),
  })
);

But I only want to delete one item from data.fields. I already tried to use this

this.setState(state => ({
  data: state.data.filter((row, j) => row.fields[x].bitname !== k),
}));

But I've not had luck yet.

Here is the data structure:

data: [
  {
    registerName: "MANID",
    address: 0,
    mode: "R",
    size: 8,
    por: 0,
    doc: "document level",
    fields: [
      { bitname: 'INT', bitmask: '16', bitvalue: '*', maskname: '*', doc: 'doc level' },
      { bitname: 'IDLE_MODE', bitmask: '0', bitvalue: '*', maskname: '*', doc: 'doc level' },
    ]
  }
]

2 Answers 2

1

You should avoid deleting the field from the existing object because that mutates state directly. Try something like this.

this.setState(
      state => (
        {
          data: state.data.map((row, j) => {
              if (j === i) {
                return {
                  ...row,
                  fields: row.fields.filter(field => field.bitname !== k)
                }                
              }
              return row;
            }),
        })); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @EvanMorrison it is working as expected now
0

If your object is row.fields[x] and you want to delete the bitname property from that object, the correct way to do it is:

delete row.fields[x].bitname

Here is an example snippet to delete a property from an object:

const field = {
  name:'Sean',
  age:25,
  bitname:'some value'
}

delete field.bitname

console.log(field)

Comments

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.