- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 immutability-helper 进行简单的创建、更新/修改、替换、删除?
如果我有一个简单值数组?
如果我有一个简单对象数组?
如果我的数组在另一个对象中?
如果我有对象的对象? (哈希,无序)
如果我有对象的 map ? (哈希,有序)
作为初学者,我找到了 official documentation有点困惑。
最佳答案
您可以在单独的问题中找到每个单独操作的答案,但不会全部列出。
<强>1。简单值数组
import update from 'immutability-helper';
const oldArray = [1, 2, 3];
// add an item
const newArray = update(oldArray, {$push: [6, 7]});
// => [1, 2, 3, 6, 7]
// modify an item
const itemIndex = 1; // modify `2` value at index `1`
const newValue = 8;
const newArray = update(oldArray, { [itemIndex]: {$set: newValue} });
// => [1, 8, 3]
// remove an item
const itemIndex = 2; // delete `3` value at index `2`
const newArray = update(oldArray, {$splice: [[itemIndex, 1]] });
// => [1, 2]
<强>2。简单对象数组
import update from 'immutability-helper';
const oldArray = [
{name: 'Stacey', age: 55},
{name: 'John', age: 77},
{name: 'Kim', age: 62},
];
// add an item
const newArray = update(oldArray, {$push: [
{name: 'Trevor', age: 45},
]});
// replace an item
const itemIndex = 1; // replace *John* at index `1`
const newValue = {name: 'Kevin', age: 25};
const newArray = update(oldArray, { [itemIndex]: {$set: newValue} });
// modify an item
const itemIndex = 1; // modify *John* at index `1`
const newArray = update(oldArray, {
[itemIndex]: {$merge, {
age: 85, // change John's age to 85
}}
});
// remove an item
const itemIndex = 0; // delete *Stacey* at index `0`
const newArray = update(oldArray, {$splice: [[itemIndex, 1]] } });
您可以使用内置的 findIndex()根据项目的属性查找项目的索引。
<强>3。数组在另一个对象中
import update from 'immutability-helper';
const oldData = {
city: {
people: [
{name: 'Stacey', age: 55},
{name: 'John', age: 77},
{name: 'Kim', age: 62},
]
}
};
// add an item
const newArray = update(oldArray, {
city: {
people: {$push: [
{name: 'Trevor', age: 45},
]}
}
});
// replace an item
const itemIndex = 1; // replace *John* at index `1`
const newValue = {name: 'Kevin', age: 25};
const newArray = update(oldArray, {
city: {
people: {
[itemIndex]: {$set: newValue} }}
}
}
);
// modify an item
const itemIndex = 1; // modify *John* at index `1`
const newArray = update(oldArray, {
city: {
people: {
[itemIndex]: {$merge, {
age: 85, // change John's age to 85
}}
}
}
});
// remove an item
const itemIndex = 0; // delete *Stacey* at index `0`
const newArray = update(oldArray, {
city: {
people: {$splice: [[itemIndex, 1]] }
}
});
<强>4。对象的对象(散列,无序)
import update from 'immutability-helper';
const oldData = {
'hash-stacey': {name: 'Stacey', age: 55},
'hash-john': {name: 'John', age: 77},
'hash-kim': {name: 'Kim', age: 62},
};
// add an item
const newData = update(oldData, {
['hash-trevor']: {$set: {name: 'Trevor', age: 45} }
});
// replace an item at a specific hash
const itemHash = 'hash-john';
const newValue = {name: 'Kevin', age: 25};
const newData = update(oldData, { [itemHash]: {$set: newValue} });
// modify an item
const itemHash = 'hash-john';
const newData = update(oldData, {
[itemHash]: {$merge: {
age: 85, // change John's age to 85
}}
});
// remove an item
const itemHash = 'hash-stacey';
const newData = update(oldData, {$unset: [itemHash] });
对象映射(散列,有序)
import update from 'immutability-helper';
const oldData = new Map([
['hash-stacey', {name: 'Stacey', age: 55}],
['hash-john', {name: 'John', age: 77}],
['hash-kim', {name: 'Kim', age: 62}],
]);
// add an item
const newData = update(oldData, {$add: [
['hash-trevor', {name: 'Trevor', age: 45}],
]});
// replace an item at a specific hash
const itemHash = 'hash-john';
const newValue = {name: 'Kevin', age: 25};
const newData = update(oldData, { [itemHash]: {$set: newValue} });
// modify an item
const itemHash = 'hash-john';
/* please edit with better single update $merge or $apply */
const newValue = update(oldData.get(itemHash), {$merge: {
age: 85, // change John's age to 85
}});
/* typescript needs to do `oldData as any` cast here */
const newData = update(oldData, { [itemHash]: {$set: newValue} });
// remove an item
const itemHash = 'hash-stacey';
const newData = update(oldData, {$remove: [itemHash] });
关于javascript - 将 immutability-helper 与数组、对象、映射一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55436821/
为什么我的简单对象不使用 Immutable.fromJS({}) 转换为 Immutable.Map() 这是 Map() - 按预期工作 > var mymap = Immutable.Map({
如何在Immutable.js列表的任意位置插入元素? 最佳答案 您正在寻找 splice method: Splice returns a new indexed Iterable by repla
https://facebook.github.io/immutable-js/docs/#/is 我尝试比较 2 个 OrderedMap,但 Immutable.is 函数不起作用。 它只能比较
对于标准的 JS 对象,可以使用解构赋值,例如: let obj = {name: 'james', code: '007'} let {name, code} = obj // creates ne
如何获取 immutable.js 映射的第一个键(不是值)? 基本上myMap.first()将返回值,但我对 key 感兴趣...... 我可以先执行 forEach 并存储,但必须是更好的方法!
查看this ,我认为一成不变。 Record是表示“javascript不可变对象(immutable对象)”的数据结构,但我想一次更新多个字段,而不是每次创建多个调用set的对象。 我想做这样的事
为什么可以改变 scala.collection.immutable.SortedMap 和 scala.collection.immutable.TreeMap? scala> import sca
好的,我有一个对象列表,非常标准。 const list = Immutable.List([{type:'thang',data:{id:'pants'}}]); 现在我想把裤子换成短裤……所以我在
如何在 OrderedMap 的末尾添加项目? 我试过了 this.boxes = this.boxes.setIn(data); 感谢您的帮助 最佳答案 一种可能的方法是使用 concat
在开始使用 reactjs 和 redux 进行开发之后,我认为在使用 redux 的同时使用 immutable.js 会更好。 但是...也许我是智障,或者在正确使用它之前需要一些练习,一切都崩溃
我要在 Immutable.js 映射上进行一系列更改。 我应该在什么时候更喜欢使用 withMutations而不是创建中间的不可变映射? 来自 Immutable.js 文档: If you ne
我想使用 Immutable 的流类型定义来描述 map 的形状。 您可以通过以下方式描述物体的形状: const stateShape: { id: number, isActive: bo
这两个 NuGet 包有什么区别?如果我正在创建 .net 4.51 项目,我应该使用其中一个吗?我过去使用过 Microsoft.Bcl.Immutable 的测试版,但只是想知道其中一个是否会比另
基本上我想要做的是,如果您按下按钮,那么条目应该获得一个新的 CEntry。如果有人可以帮助我,那就太好了。谢谢! struct AView: View { var entries = [CEn
我有来自框架的协议(protocol) public protocol BaseMappable { /// This function is where all variable mappi
Swift 开发相对较新。尝试修改其他人编写的几个函数。目标是在用户登录后第一次调用此路径(该部分尚未设置)时离开原始路径,并在后续调用中使用其他路径。 因此,在编写将指向首次通过或非首次通过的逻辑之
这段代码 extension Collection { mutating func f() { removeFirst() } } 处理错误 cannot use mutating m
我正在处理一些使用许多不同项目“列表”的 NodeJS 代码。底层实现使用包括 Immutable.List、Immutable.Set、Javascript Array 和 Javascript S
我正在尝试使用 Reacts Immutability helpers 或 Immutable js 合并一个不可变的对象数组。我正在尝试创建一个仅包含名称已更改的记录的列表。如果名称在提交前已多次更
我被困在一个 Cannot use mutating member on immutable value: function call returns immutable value 这段代码错误:
我是一名优秀的程序员,十分优秀!