javascript ImmutableJS - 更新列表中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31767313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:20:15  来源:igfitidea点击:

ImmutableJS - update value in a List

javascriptreactjsimmutable.js

提问by user3696212

I have a Map like this (in ImmutableJS):

我有一个这样的地图(在 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"

我想更新“arrayOfValues”数组中第二个条目中的“inside”值。我该怎么做?这就是我现在所拥有的,它说“未捕获的错误:无效的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.

经过几个小时的寻找解决方案,我感谢任何帮助。谢谢你。

回答by Brian Park

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

你在做什么是正确的(见这个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 origdirectly. That's the whole purpose of this Immutablelibrary. It doesn't mutate the existing data but creates a new one from the existing one.

当您调用 时orig.setIn(),它不会orig直接修改。这就是这个Immutable图书馆的全部目的。它不会改变现有数据,而是从现有数据创建一个新数据。

回答by Brian Kent

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

您的setIn示例如您在此 plunkr 中看到的那样工作:http://plnkr.co/edit/1uXTWtKlykeuU6vB3xVO?p=preview

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

也许您假设 的值theMap将因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');

由于这些结构是不可变的,因此您必须在新变量中捕获修改后的值,如 var theMap2 = theMap.setIn(['arrayOfValues',1,'one','inside'],'updated value');

回答by Tauqeer Hassan

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

activePane 是我必须修改的 Array(List) 中对象的索引

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);