How to get first n elements of an object using lodash?

后端 未结 3 1509
长发绾君心
长发绾君心 2021-02-19 21:33

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\'

3条回答
  •  滥情空心
    2021-02-19 22:16

    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 };
    

    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?

    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.

    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) );

    This is using Object.keys, Array.prototype.sort, and Array.prototype.reduce

    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) );

    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.

提交回复
热议问题