当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Update Immutable List with only new data that doesn't match prior nested keys?

问题描述:

I have an immutableJS List which I would like to append to only if the new data's location.name does not match my prior data's location.name.

I get initial data into the List in this format (new data is in the same format, an array of objects):

[

{

"location": {

"name":"A String"

}

},

{

"location": {

"name":"A String 2"

}

}

]

However, any subsequent data, I'd like to append to this array of objects ONLY if the location name doesn't match any prior location names that already exist in the state.

Currently I'm returning the state as such but there is a lot of repetition in this approach.

case RECEIVE_LOCATIONS:

return state

.set('stores', state.get('stores').update(fromJS(action.locations)))

网友答案:

you can filter out the repeated location items

case RECEIVE_LOCATIONS:  {
  const newLocations = fromJS(action.locations).filter((newItem) => (
    !state.get('stores').find((oldItem) => (
      oldItem.location.name === newItem.location.name
    )
  );

  return state.set('stores', state.get('stores').update(newLocations));
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: