2017-01-05 3 views
0

immutablejsのドキュメントを理解するのは非常に困難です。 Sanskarという名前のリストを更新したいと思います。そのために私は最初にfindIndexを使ってインデックスを探し、update()を使ってそれを更新しようとしました。しかし、私はitem.get()のエラーを取得している関数ではありません。immutablejsを使用してリストから名前を更新

なぜ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()); 

私はjsbin

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

答えて

1

でimmutablejsを練習し、あなたが何をしたいのか行う方法の例については、以下のコードを参照してくださいしています。最初の問題は、リストを正しく作成していないことです.2番目は、問題の要素を更新する方法です。

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()); 
+0

ありがとうございました。 – Serenity

+1

NP!それが助けてくれてうれしい。 –

関連する問題