2

i am trying to check my inner array id same as dispatched id, table example

{
 _id :1,
 name: sagar elias jacky
 Amenities :[{ id: 100, title : hi },{ id: 101, title : hallo } ]
}

checking dispatched id exit or not using map,

return { ...state, 
  items : {...state.items, 
  Amenities : { ...state.items.Amenities
 .map(x=> x._id === action.dispatchedID ?  {...x,deleting: true} :  x ) }}}

but it will return with non array Amenities, like

Amenities:
0: { id: 100, title : hi },
1: { id: 101, title : hallo }

i want this to

Amenities:Array(2)
0: { id: 100, title : hi },
1: { id: 101, title : hallo }
1
  • returned array looks the same as your output, can you create demo to reproduce an issue? Commented Mar 1, 2019 at 8:03

1 Answer 1

2

When you spread an array inside {}, it creates an object with indexes of array as keys

const array = [{a:1}, {a:2}]
console.log({...array})

So, change

Amenities : { ...state.items.Amenities
 .map(x=> x._id === action.dispatchedID ?  {...x,deleting: true} :  x ) }

to:

Amenities : [ ...state.items.Amenities
 .map(x=> x._id === action.dispatchedID ?  {...x,deleting: true} :  x ) ]
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.