0

Say I have object:

obj {
  1:{
    index: 3
  },
  2:{
    index: 1
  },
  3:{
    index: 2
  },
  4:{
    index: 0
  }
}

I want to convert it into an array but in orders of "index", so the output should be

[ { index: 0 }, { index: 1 }, { index: 2 }, { index: 3 } ]

So like _.values but ability to sort by a property?

1
  • There is no need to do _.values etc if you use lodash. Check my answer it is just _.sortBy or _.orderBy really since they work on objects etc. Commented Oct 22, 2018 at 16:43

2 Answers 2

1

You won't find a single method to do everything you want. Instead you should divide your operation into smaller chunks and chain them. So as you wrote:

like _.values but ability to sort by a property

You write about two operations: extract values from a map (using _.values) and then sort the resulting array by a property (this can be done by _.sortBy).

To start a chain of operations in Lodash use _.chain and to materialize a chain use chain.value().

Snippet:

const obj = {
    1:{
        index: 3
    },
    2:{
        index: 1
    },
    3:{
        index: 2
    },
    4:{
        index: 0
    }
};

const result = _.chain(obj)
    .values()
    .sortBy('index')
    .value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that was what I was looking for. Question, what does the '.value()" in the end of chain() do? And what are the advantages of using chain over doing it like this? "let result = _.sortBy(_.values(obj), 'index')"
Whenever you chain operations you need to call value() to get results from it. It's because Lodash needs to know when you've finished and just want to get the result. Regarding the second question: mainly readibility. Try reading these two statements in both forms and see for yourself. Then try doing the same for let's say 10 operations. You'll definitely agree then that the chain form is clearer and more elegant.
1

You really do not need a library for this since in plain JS it is practically the same with just Object.values and then sort:

const obj = { 1:{ index: 3 }, 2:{ index: 1 }, 3:{ index: 2 }, 4:{ index: 0 } }

const result = Object.values(obj).sort((a,b) => a.index - b.index)

console.log(result)

But if you really need lodash then this really is just orderby or sortBy since they work on objects with no need to do the values etc:

const obj = { 1:{ index: 3 }, 2:{ index: 1 }, 3:{ index: 2 }, 4:{ index: 0 } }

const result = _.orderBy(obj, 'index')  // or _.sortBy

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

1 Comment

That is awesome, did not know that at all. Thank you for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.