0

it is deleting all objects except last one from the array instead of updating a property of all objects, want to update 'ItemDeliveryStatus' of all objects inside map function

const [arrayList, setArrayList] = useState([
        { Id: 1, Name:'A', ItemDeliveryStatus:1 },
        { Id: 2, Name:'B', ItemDeliveryStatus:1 },
        { Id: 3, Name:'C', ItemDeliveryStatus:1 },
        { Id: 4, Name:'D', ItemDeliveryStatus:1 },
      ])

const [returnCount, setReturnCount ]=useState(0)

const updateAllObjects=()=>
{
  arrayList.map(items=>
        {

            if(items.ItemDeliveryStatus==1)
            {
                setArrayList([{ ...items, ItemDeliveryStatus:4}])
            }

            if (items.ItemDeliveryStatus==4)
            {
               setReturnCount(prev=>prev+1)                        
            }
        })
}

final Result Should be like this 
([
        { Id: 1, Name:'A', ItemDeliveryStatus:4 },
        { Id: 2, Name:'B', ItemDeliveryStatus:4 },
        { Id: 3, Name:'C', ItemDeliveryStatus:4 },
        { Id: 4, Name:'D', ItemDeliveryStatus:4 },
  ])

1 Answer 1

2

You can update like this for all object:

const updateAllObjects = (value) => {
  setArrayList(
    arrayList.map((item) => {
      if (item.ItemDeliveryStatus == 1) {
        return { ...item, ItemDeliveryStatus: value };
      };
      return item;
    })
  );
};
Sign up to request clarification or add additional context in comments.

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.