Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable.js: How to find an object in an array by specify property value

I have an array made by Immutable.js:

    var arr = Immutable.List.of(
        {
            id: 'id01',
            enable: true
        },
        {
            id: 'id02',
            enable: true
        },
        {
            id: 'id03',
            enable: true
        },
        {
            id: 'id04',
            enable: true
        }
    );

How can I find the object with id: id03? I want to update its enable value and get an new array

like image 849
hh54188 Avatar asked Nov 15 '16 16:11

hh54188


1 Answers

First you need to findIndex, and then update your List.

const index = arr.findIndex(i => i.id === 'id03')
const newArr = arr.update(index, item => Object.assign({}, item, { enable: false }))

OR

const newArr = arr.update(
  arr.findIndex(i => i.id === 'id03'),
  item => Object.assign({}, item, { enable: false }) 
 )
like image 167
caspg Avatar answered Oct 12 '22 13:10

caspg