如何使用lodash获取对象的前n个元素?

编程入门 行业动态 更新时间:2024-10-19 10:20:50
本文介绍了如何使用lodash获取对象的前n个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用lodash从对象(不是数组)中获取前n个键/值对.我发现下划线的答案 ,表示使用 first (在lodash中不存在),或使用 take (仅适用于数组).

I want to get the first n key/value pairs from an object (not an array) using lodash. I found this answer for underscore, which says to use use first (doesn't exist in lodash), or to use take (only works on arrays).

尝试从对象中获取"a:7"和"b:8"对的示例节点会话:

Sample node session trying to get the 'a:7' and 'b:8' pairs from an object:

> var ld=require("lodash") undefined > var o={a:7, b:8, c:9} undefined > ld.keys(o) [ 'a', 'b', 'c' ] > ld.take(o, 2) [] > ld.first(o, 2) undefined >

当然,必须有一些简单的方法可以用lodash做到这一点,但是对于我一生,我什么也找不到.也许我必须求助于本机js?

Surely, there must be some easy way to do this with lodash, but for the life of me I can't find anything. Maybe I have to resort to native js?

推荐答案

如果不编写自定义代码,则无法获取对象的前N个元素.这是因为对象中的元素没有排序,所以如果存在是它的库函数,因此无法保证会为您提供所需的元素.给定类似

You cannot take the first N elements of an object without writing custom code. This is because there is no ordering of the elements in objects, so if there were a library function for it, it would never be guaranteed to give you the elements you expect. Given an object like

var obj = { b: 3, y: 2, a: 1 };

目前尚不清楚前2个"指的是什么-您是否要a和b,因为这是字母顺序?如果是这样,他们是否按该顺序排列?如果不是,您是否要b和y因为它们首先出现?也许您想要a和y,因为它们的值最低?

it is not clear what the "first 2" refers to - do you want a and b because that is the alphabetic order? If so are they in that order or not? If not, do you want b and y because they appear first? Perhaps you want a and y because of their values being the lowest?

不能保证您不会得到重复项,因此所有这些组合都是有效的.此外,您可以按任意顺序获得它们y,而a是同等有效的输出.您可能更喜欢一个或另一个,但通常并不能使它正确.

There is no guarantee for what you will get aside from not getting duplicates, so all of those combinations are valid. Furthermore, you can get them in any order y and a is equally valid output You may prefer one or another but it doesn't make it correct in general.

有很多解决方法,但是您必须接受需要处理非订单的情况.

There are ways around this and but you have to accept that you need to deal with the non-order.

纯JavaScript解决方案.

Pure JavaScript solution.

function firstN(obj, n) { return Object.keys(obj) //get the keys out .sort() //this will ensure consistent ordering of what you will get back. If you want something in non-aphabetical order, you will need to supply a custom sorting function .slice(0, n) //get the first N .reduce(function(memo, current) { //generate a new object out of them memo[current] = obj[current] return memo; }, {}) } var obj = { b: 2, y: 25, a: 1 } console.log( firstN(obj, 2) );

这正在使用 Object.keys , Array.prototype.sort ,和 Array.prototype.reduce

使用lodash可以实现相同的功能,但是没有比这更简洁的了-它涉及调用相似的功能.可以这样做,例如:

The same can be achieved with lodash but not vastly more concise than this - it would involve calling similar functionality. It can be done like this, for example:

function firstN(obj, n) { return _.chain(obj) .keys() .sort() .take(n) .reduce(function(memo, current) { memo[current] = obj[current]; return memo; }, {}) .value(); } var obj = { b: 2, y: 25, a: 1 } console.log( firstN(obj, 2) );

<script src="cdnjs.cloudflare/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

如您所见,它与以前几乎相同.语法和 exact 的用法可能有所不同,但是主要要点仍然存在-您需要进行排序以确保一致性,然后然后可以得到任意数量的物品.

As you can see, it's pretty much the same as before. There can be variations on the syntax and the exact means of how you do this task, but the major points should still be there - you need to sort for consistency and then you can get any number of items.

更多推荐

如何使用lodash获取对象的前n个元素?

本文发布于:2023-11-30 04:22:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   元素   对象   lodash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!