简体   繁体   English

lodash 确实省略了对其调用的数组的 mutate 吗?

[英]lodash does omit mutate the array on which it is called?

Looking at filter method of lodash, it was unclear to me by docs whether this method mutates the collection on which it is called.查看 lodash 的filter方法,我不清楚这个方法是否会改变调用它的集合。

But it appears that in react, when I try to make a new collection from previous properties and _.omit does mutate the parent.但似乎在反应中,当我尝试从以前的属性创建一个新集合时, _.omit确实改变了父级。

Is _.reject a more proper solution which does not mutate its parent? _.reject是一个更合适的解决方案,它不会改变它的父级吗?

here is an example jsfiddle of profile.id dissapearing.这是 profile.id 消失的示例 jsfiddle。 https://jsfiddle.net/69z2wepo/96535/ https://jsfiddle.net/69z2wepo/96535/

I don't completely follow your question, but it appears you are concerned with whether one or more of filter , omit or reject mutate their input.我没有完全理解您的问题,但您似乎关心filteromitreject一个或多个是否会改变他们的输入。 None of them do.他们都没有。 Run this snippet to see:运行此代码段以查看:

 const original = {a: 1}; console.log('--- filter() ---') console.log(_.filter(original, 'a')); console.log(original); console.log('--- omit() ---') console.log(_.omit(original, 'a')); console.log(original); console.log('--- reject() ---') console.log(_.reject(original, () => true)); console.log(original);
 <script src="https://unpkg.com/lodash@4.17.4/lodash.js"></script>

Shameless plug: filter() and omit() are available in my library Micro-dash , with a much smaller bundle size than their Lodash counterparts.无耻的插件: filter()omit()在我的库Micro-dash中可用,与它们的 Lodash 对应的包大小相比要小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM