如何使用lodash获取对象的前n个元素? [英] How to get first n elements of an object using lodash?

查看:449
本文介绍了如何使用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个"指的是什么-您是否要ab,因为这是字母顺序?如果是这样,他们是否按该顺序排列?如果不是,您是否要by因为它们首先出现?也许您想要ay,因为它们的值最低?

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="https://cdnjs.cloudflare.com/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个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆