gpt4 book ai didi

javascript - Immutable.js:如何通过指定属性值在数组中查找对象

转载 作者:数据小太阳 更新时间:2023-10-29 04:04:09 26 4
gpt4 key购买 nike

我有一个由 Immutable.js 制作的数组:

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

如何找到 id: id03 的对象?我想更新它的 enable 值并获得一个新数组

最佳答案

首先你需要findIndex , 然后 update你的 list 。

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

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

关于javascript - Immutable.js:如何通过指定属性值在数组中查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40615035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com