jhohlfeld jhohlfeld - 6 months ago 48
Javascript Question

In lodash, what is the opposite of `pairs()`?

I cant't see it in the docs - http://lodash.com/docs - please help me:

What is the opposite of

_.pairs()
? There must be one - I just cant' see where!

The goal is to produce key/value pairs from an array of the form
[['key1':'value1'], ['key2':'value2'], ...]


Not that I really need it a lib for that: I just like to re-use things..

Answer

I think you're looking for _.object/_.zipObject.

http://lodash.com/docs#zipObject

"use strict";

var obj, pairs, objResult;

obj = {
    key1: "value1",
    key2: "value2"
};

pairs = _.pairs(obj);
objResult = _.object(pairs);

// The original object.
console.log(obj);
// The object as an array of arrays.
console.log(pairs);
// The array of arrays converted back to the original object.
console.log(objResult);

http://jsfiddle.net/HmDk6/

Comments