最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ImmutableJS - update value in a List - Stack Overflow

programmeradmin4浏览0评论

I have a Map like this (in ImmutableJS):

{arrayOfValues: [
  {one: {inside: 'first in array'}},
  {one: {inside: 'second in array'}}
]}

And I want to update the value "inside" in the second entry in the "arrayOfValues" array. How can I do it? This is what I have now and it says "Uncaught Error: invalid keyPath"

theMap.update('arrayOfValues',(list)=>{
  return list.setIn([1,'one','inside'],'updated value'); 
})

I also tried directly this and it didn't work:

theMap.setIn(['arrayOfValues',1,'one','inside'],'updated value');

After several hours of looking for the solution, I appreciate any help. Thank you.

I have a Map like this (in ImmutableJS):

{arrayOfValues: [
  {one: {inside: 'first in array'}},
  {one: {inside: 'second in array'}}
]}

And I want to update the value "inside" in the second entry in the "arrayOfValues" array. How can I do it? This is what I have now and it says "Uncaught Error: invalid keyPath"

theMap.update('arrayOfValues',(list)=>{
  return list.setIn([1,'one','inside'],'updated value'); 
})

I also tried directly this and it didn't work:

theMap.setIn(['arrayOfValues',1,'one','inside'],'updated value');

After several hours of looking for the solution, I appreciate any help. Thank you.

Share Improve this question asked Aug 1, 2015 at 23:50 user3696212user3696212 3,4395 gold badges20 silver badges36 bronze badges 2
  • Your example works for me without errors. – Artem Commented Aug 2, 2015 at 0:45
  • you can't update immutable structures. – dandavis Commented Oct 16, 2015 at 18:40
Add a comment  | 

3 Answers 3

Reset to default 15

What you are doing is correct (see this JSBin).

const orig = Immutable.fromJS({
  arrayOfValues: [
    { one: { inside: 'first in array' } },
    { one: { inside: 'second in array' } },
  ]
});

const updated = orig.setIn(['arrayOfValues', 1, 'one', 'inside'], 'updated value');

console.log(updated.toJS());

// {
//   arrayOfValues: [
//     { one: { inside: 'first in array' } },
//     { one: { inside: 'second in array' } },
//   ]
// }

When you call orig.setIn(), it doesn't modify orig directly. That's the whole purpose of this Immutable library. It doesn't mutate the existing data but creates a new one from the existing one.

Your setIn example works as you should see in this plunkr: http://plnkr.co/edit/1uXTWtKlykeuU6vB3xVO?p=preview

Perhaps you are assuming the value of theMap will be changed as a result of the setIn?

As these structures are immutable, you must capture the modified value in a new variable as var theMap2 = theMap.setIn(['arrayOfValues',1,'one','inside'],'updated value');

activePane is the index of Object in Array(List) that I had to modify

case CHANGE_SERVICE:
  var obj = {
    title: '1212121 Tab',
    service: '',
    tagName: '',
    preDefinedApi: '',
    methodType: '',
    url: '',
    urlParams: [{
      label: '',
      name: '',
      value: '',
    }],
    headers: [{
      label: '',
      name: '',
      value: '',
    }],
  };
  var activePane = state.get('activePane');
  var panes = state.setIn(['panes', activePane, 'service'], action.val);

  return state.setIn(['panes', activePane, 'service'], action.val);
发布评论

评论列表(0)

  1. 暂无评论