score:2

Accepted answer

so, what do you need here is nested update. at first, you have to check your list of loadedaccounts whether it has this account or not. secondly, you have to change activeaccount field. and, lastly, add (or update) account to loadedaccounts.

the caveat here is how you pass account property. if you derive it from somewhere and pass around as a record, you can just compare by === (or by .equals()), but it seems that it is just a plain javascript object – i'll suppose it later.

in terms of code it would be something like:

// we can do it by different ways, it is just one of them
const listwithloadedaccounts = state.get('loadedaccounts');
const isaccountalready = boolean(
  listwithloadedaccounts.filter(
    account => account.get('id') === action.account.id
  ).size
);

const patchedstate = state.set('activeaccount', action.account.id);
return isaccountalready
  ? patchedstate.updatein(['loadedaccounts'], list => list.map(account => account.get('id') === account.action.id ? new account(action.account) : account))
  : patchedstate.updatein(['loadedaccounts'], list => list.concat(new account(action.account)))

it is not the ideal code, something can be deduplicated, but you get the idea – always use deep merge / update if you need to change nested fields or data structures.

you also can set new field directly, like:

const oldlist = state.get('loadedaccounts');
const newlist = oldlist.concat(action.account);
const patchedstate = state.set('loadedaccounts', newlist);

but i personally find that it is not that flexible and also not consistent, because it is quite common operation to perform deep merge.

score:-1

since this is the top search result for what is in the title of the question

an example of how to add to a list using immutable.js:

let oldlist = list([ 1, 2, 3, 4 ])
let newlist = oldlist.push(5)

insert()

returns a new list with value at index with a size 1 more than this list. values at indices above index are shifted over by 1. insert(index: number, value: t): list discussion

this is synonymous with list.splice(index, 0, value).

list([ 0, 1, 2, 3, 4 ]).insert(6, 5)
// list [ 0, 1, 2, 3, 4, 5 ]

score:0

i hope this example will help, i am creating a new immutable list and first performing an update and then adding a new element. i am not passing the object which i want to replace with, but you can also pass your existing object, also in update method you have access to current item

class test {
  a = null;
  b = null;
 constructor(a,b){
 this.a=a;
 this.b=b;
 }
}

$("#test").html("");

function logme(item){
  $("#test").append("<br/>"+json.stringify(item));
 }
function logmenewline(){
  $("#test").append("<br/>");
 }

function listaddupadte(key){ 
  
     var index= list.get('data').findindex(listing => {
    return listing.a === key;
  });
  
  logme(' found index (-1 for not found) : ' + index);
  
  if(index >= 0){
    logme("upadte");
    list = list.set("data",list.get("data").update(index,function(item){
    return new test(key,"go");
    }));
  }else {
    
    logme("add");
    list = list.set("data",list.get("data").push(new test(key,"go")));
  }

  list.get('data').foreach(item=>{
    logme(item);
  });
}


var list = immutable.fromjs({
"data":[new test(6,"abhi"),new test(4,"raj"),new test(1,"ajay")]
});

logme("intial data");
list.get('data').foreach(item=>{
  
logme(item);
  
});




logmenewline();
logme("testing replace with a =  4 ")
logmenewline();
listaddupadte(4);


logmenewline();
logme("testing add with a = 8 ")
logmenewline();
listaddupadte(8);
logmenewline();
logmenewline();
logmenewline();
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.2/immutable.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test"></div>


Related Query

More Query from same tag