简体   繁体   English

使用immutablejs从列表更新名称

[英]update name from the list using immutablejs

I am having very hard time to understand the documentation of immutablejs. 我很难理解immutablejs的文档。 I want to update the list whose name is Sanskar. 我想更新名称为Sanskar的列表。 For that i first tried to use findIndex to find its index and update it using update(). 为此,我首先尝试使用findIndex查找其索引并使用update()更新它。 But i am getting an error of item.get() is not a function. 但我收到item.get()的错误不是一个函数。

Why i am getting an error of item.get is not a function? 为什么我收到item.get错误不是函数?

const arr = [
             {name: 'Sanskar', age: 24, designation: 'Intern Developer'},
             {name: 'John', age: 28, designation: 'Developer'}
            ]; 
const list1 = Immutable.List.of(arr);

const list2 = list1.findIndex(item => item.get('name') === 'Sanskar');
console.log('list2', list2.toJS());

I am practicing immutablejs in jsbin 我在jsbin中练习immutablejs

http://jsbin.com/zawinecutu/edit?js,console http://jsbin.com/zawinecutu/edit?js,控制台

Please see the code below for an example of how to do what you want to do. 请参阅下面的代码,以获取有关如何执行所需操作的示例。 The first issue is that you're not building your List correctly, the second is the way you're updating the element in question. 第一个问题是您没有正确构建列表,第二个问题是更新有问题的元素的方式。

const Immutable = require('immutable');
const arr = [
             {name: 'Sanskar', age: 24, designation: 'Intern Developer'},
             {name: 'John', age: 28, designation: 'Developer'}
            ];

// your initial data is an array of objects.  If you want a List of objects:
const list1 = Immutable.List.of(...arr);

// the contents of the list are ordinary JavaScript objects.
const person2Index = list1.findIndex(item => item['name'] === 'Sanskar');

// now you can use the List.get() method:
const person2 = list1.get(person2Index);

person2['designation'] = 'Astronaut';

// do the update like this
const list2 = list1.update(person2Index, p => person2);

console.log(list2.toJS());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM