2017-01-05 2 views
0

Мне очень трудно понять документацию immutablejs. Я хочу обновить список, чье имя - Санскар. Для этого я сначала попытался использовать 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()); 

Я практикую immutablejs в jsbin

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

ответ

1

Пожалуйста, смотрите ниже код для примера того, как сделать то, что вы хотите сделать. Первая проблема заключается в том, что вы неправильно строите свой список, второй - способ обновления данного элемента.

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! Рад, что это помогло. –

Смежные вопросы