javascript 有没有办法在 lodash 中使用 immutable.js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30109639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:42:41  来源:igfitidea点击:

Any way to use immutable.js with lodash?

javascriptreactjsimmutabilitylodashimmutable.js

提问by Glen Swift

I'm using immutable.jswith my flux application. It is very useful and gives performance boost. But what actually makes me sad is the fact that I can't use lodashtogether with it. Lodash provides great API with ton of useful functions, so I wonder maybe there is a way to get them together to work for me?

我在我的通量应用程序中使用immutable.js。它非常有用,可以提升性能。但真正让我难过的是我不能将lodash与它一起使用。Lodash 提供了很棒的 AP​​I 和大量有用的功能,所以我想知道是否有办法将它们组合在一起对我来说有效?

采纳答案by Brian Neisler

I've recently written a Lodash wrapper providing Immutable.JS support called mudash. Most of the major Lodash functions are supported with more being added regularly.

我最近编写了一个 Lodash 包装器,提供名为mudash 的Immutable.JS 支持。支持大多数主要的 Lodash 功能,并定期添加更多功能。

回答by Brett Hayes

You could use Ramdaif you want immutability and side-effect free functions.

如果你想要不变性和无副作用的函数,你可以使用Ramda

The library provides useful functions similar to lodash but in a functional programming style that never mutates user data.

该库提供了类似于 lodash 的有用函数,但采用了从不改变用户数据的函数式编程风格。

Consider this React exampleusing the flatten function in Ramda using ES6 syntax.

考虑这个使用 ES6 语法在 Ramda 中使用 flatten 函数的React 示例

import { flatten } from 'ramda';  

const users = ['Steve Jobs', ['Steve Wozniak']];
const flatUsers = flatten(users);

// returns ['Steve Jobs', 'Steve Wozniak']

回答by waterproof

I'm assuming you're trying to do something like do a lodash map on an immutable set. Lodash doesn't do helpful things when you try to do that directly.

我假设你正在尝试做一些事情,比如在一个不可变的集合上做一个 lodash 地图。当您尝试直接执行此操作时,Lodash 不会做有帮助的事情。

Note that immutable.js already has a lot of its own manipulation functions (like map) as well as its own lazy chaining (via Seq) so you should look into those.

请注意, immutable.js 已经有很多自己的操作函数(如map)以及自己的惰性链接(via Seq),因此您应该研究一下。

If you need to do something that lodashprovides and immutable.jsdoes not, one thing you can do is take your immutable object and convert it to a vanilla JS object for consumption by lodash. For instance:

如果你需要做一些lodash提供和immutable.js不提供的事情,你可以做的一件事就是把你的不可变对象转换成一个普通的 JS 对象供 lodash 使用。例如:

// Do all of your fancy immutable.js stuff...
my_set = immutable.Set([1,2,3]).union(immutable.Set([2,3,4]))
// ...and then convert to JS before you do all of your fancy lodash stuff
mapped_set = _(my_set.toArray()).map(whatever)

You'll of course have to take performance into account here, since you may end up with the worst of both worlds if you convert from one to the other by copying your data into a vanilla data structure. In the toy case above, for instance, you'd probably be better off using an immutable.js map()directly.

您当然必须在这里考虑性能,因为如果您通过将数据复制到普通数据结构中来从一个转换到另一个,那么您最终可能会遇到两个世界中最糟糕的情况。例如,在上面的玩具案例中,您最好map()直接使用 immutable.js 。

回答by Artem Gavrysh

How about to use withMutationsin ImmutableJS?

在 ImmutableJS 中使用withMutations怎么样?

Search for Batching Mutationsfor brief explanation here.

在此处搜索Batching Mutations简要说明。

回答by bryanph

seamless-immutableaims to provide an API that is backwards compatible with vanilla JS data structures.

无缝不可变旨在提供一个向后兼容 vanilla JS 数据结构的 API。

This allows you to use lodash methods. However, since many of lodash's methods are written with mutability in mind, they are probably far from optimal.

这允许您使用 lodash 方法。但是,由于 lodash 的许多方法在编写时都考虑到了可变性,因此它们可能远非最佳。

回答by engineforce

You may want to check out https://github.com/engineforce/ImmutableAssigncreated by me, which is a light weigh immutable helper, which supports immutability and allows you to continue working with POJO (Plain Old JavaScript Object). Therefore you can use any lodash functions.

您可能想查看由我创建的https://github.com/engineforce/ImmutableAssign,这是一个轻量级的不可变助手,它支持不变性并允许您继续使用 POJO(Plain Old JavaScript Object)。因此,您可以使用任何 lodash 函数。

E.g.,

例如,

var iassign = require("immutable-assign");
var _ = require("lodash");

var o1 = { a: { c: 1 }, b: [1, 2, 3] };
var o2 = iassign(
    o1, 
    function(o) { return o.b; },   // get property to be updated 
    function(b) {                  // update select property
        return _.map(b, function(i) { return i + 1; }); 
    }
);


// o2 =  { a: { c: 1 }, b: [2, 3, 4] }
// o1 is not modified

// o2 !== o1
// o2.b !== o1.b

// o2.a === o1.a