reactjs - How can I remove some element in List with Immutable? -


i use react + redux immutable, , how can remove element in list immutable?

import immutable, { list, map, fromjs } 'immutable';  let initialstate = fromjs({     floor: [         {             floor: '1',             rooms: [                 {                     room: '101'                 },                 {                     room: '102'                 },                 {                     room: '103'                 }             ]         }     ] });  export default function list(state = initialstate, action) {     switch (action.type) {         //remove room         case remove_room:             //how can remove element in list immutable?         default:             return state;     } }; 

then, dispatch action remove room dispatch(removeroom('102')), how can remove {room: 102} immutable?

first, write function findroompath find room path in state.

and use deletein remove value @ path.

var roompath = findroompath(state, 101); state = state.deletein(roompath); 

var state = immutable.fromjs({      floor: [          {              floor: '1',              rooms: [                  {                      room: '101'                  }              ]          },        {              floor: '2',              rooms: [                  {                      room: '104'                  }              ]          }      ]  });    function findroompath(state, room) {    var keypath = ['floor'];    state      .get('floor')      .find((v, k1) => {        return v          .get('rooms')          .find(function(v, k2) {            if (+v.get('room') === +room) {              keypath.push(k1, 'rooms', k2);              return true;            }          });            });      return keypath.length > 1 ?      keypath :      null;  }    function deleteroom(state, room) {    var roompath = findroompath(state, room);    return (roompath) ?      state.deletein(roompath) :      state;  }      console.log(deleteroom(state, 101).tojs());
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>


Comments