4

Searched and searched, can't find this, but I'm assuming it's easy.

I'm looking for the lodash "object" equivalent of lodash _.pairs() - but I want an array of objects (or a collection of objects).

Example:

// Sample Input
{"United States":50, "China":20}

// Desired Output
[{"United States":50}, {"China":20}]

3 Answers 3

6

Would something like this be sufficient? It's not lodash, but...

var input = {"United States":50, "China":20};
Object.keys(input).map(function(key) {
  var ret = {};
  ret[key] = input[key];
  return ret;
});
//=> [{"United States":50}, {"China":20}]
Sign up to request clarification or add additional context in comments.

5 Comments

Nice solution. I'm going to wait and see if a lodash option comes up - this project is fairly committed to using lodash if possible.
lodash is helper lib, you dont need to use it if your language natively supports the thing you are trying to do :P
@cale_b All javascript is lodash, but trivially, of course.
@mkoryak - Understood. But, if I have lodash loaded, and I can complete this in a single function line such as _.pairs(), and use it in my _.chain(), then using the lodash method makes far more sense than for me to write this function and call it separately. Are you suggesting it's not available in lodash?
@cale_b Vohuman has provided exactly the solution you were looking for!
5

Using lodash, this is one way of generating the expected result:

var res = _.map(obj, _.rearg(_.pick, [2,1]));

The above short code snippet can be confusing. Without using the _.rearg function it becomes:

_.map(obj, function(v, k, a) { return _.pick(a, k); });

Basically the rearg function was used for reordering the passed arguments to the pick method.

4 Comments

this is the correct answer to this question, but if you use this in my code base without a 10 line comment, I will kick you. I can look at @HunanRostomyan's code and tell you exactly what it does without much thought
@mkoryak - indeed, lots of lodash function chains require comments, otherwise it's virtually nonsensical! (I'm finding this to be true of Angular also).
@cale_b, to use the code in chain you need to add it as a mixin.
@andy - I was able to make it work in a chain without mixin - _.chain(data).map(function(v,k,a) {return _.pick(a,k);});
1

ok, if you must:

var input = {"United States":50, "China":20};
var ouput = _.map(input, function(val, key){ var o = {}; o[key] = val; return o; });

but this is not better than the previous answer. its worse.

3 Comments

You are right, it's not better. While I know I could use _.map to do about anything, what I'm asking is if there's a version equivalent to _.pairs() that I'm missing....
Thanks for the help on this one!
@mkoryak: care to check qtip question? stackoverflow.com/questions/34125268/…

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.