0

I am struck in nested array and need help: my data is:

Data: [{
    name: "Steel Support, Aviation Lights, Std", // match parent array name
    code: "450W0619P001",
    reserved: [{
        serial: "one",
        selected: false // toggle this
      },
      {
        serial: "two",
        selected: true, // toggle this
      },
    ],
  },
  {
    name: "Lock Nut, Prevailing Torque, M12x1.75m", // match parent array name
    code: "450W0619P001",
    reserved: [{
        serial: "one",
        selected: true // toggle this
      },
      {
        serial: "two",
        selected: true // toggle this
      },
      {
        serial: "three",
        selected: false // toggle this
      },
    ],
  },
]

const [erpParts, setErpParts] = useState(Data);

Here is my function to toggle nested array object state. I need to match parent array name and then toggle child array object value.

const selectPart = (parentArr, part) => {
  setErpParts(
    erpParts.map((x) => {
      if (x.name !== parentArr.name) return x;
      return { ...x,
        reserved: [...x.reserved, part.selected != part.selected]
      };
    })
  );
};

My function is not showing desired result. please look into this and help.

1 Answer 1

1

This line will create a copy of reserved array and will add a new value boolean false to the copy:

reserved: [...x.reserved, part.selected != part.selected]

You have to map every item inside your array and updates its property selected:

  const selectPart = (parentArr, part) => {
  setErpParts(erpParts =>
    erpParts.map(x => {
      if (x.name !== parentArr.name) return x
      else {
        const reservedUpdated = x.reserved.map(res => {
          if (res.serial === part.serial) {
            return {
              ...res,
              selected: !part.selected
            }
          } else {
            return res
          }
        })
        return {
          ...x,
          reserved: reservedUpdated
        }
      }
    })
  )
}
Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately it is not working. first time it is toggling the state but when I toggle again it toggled all objects of reserved array
So your goal is toggling one object instead all?
When you want to change the state using the old state like toggling you should use callback instead of an object, you can try the last version of my answer
still no luck :( I am seeing the same issue. this is how I am calling the function you mentioned: <View> {erpParts.map((part, i) => ( <View key={i}> {part.reserved.map((currPart, i) => ( <View key={i}> <Text>{currPart.serial}</Text> <Text>{currPart.selected ? "true" : "false"}</Text> <Button onPress={() => selectPart(part, currPart)}>Toggle</Button> <Divider /> </View> ))} </View> ))} </View>
first time it works fine but when I try to toggle again it toggle whole reserved array objects.
|

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.