javascript 在 lodash 中,`pairs()` 的反义词是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20716075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In lodash, what is the opposite of `pairs()`?
提问by jhohlfeld
I cant't see it in the docs - http://lodash.com/docs- please help me:
我在文档中看不到它 - http://lodash.com/docs- 请帮助我:
What is the opposite of _.pairs()
? There must be one - I just cant' see where!
的反义词是_.pairs()
什么?一定有一个——我就是看不到在哪里!
The goal is to produce key/value pairs from an array of the form [['key1':'value1'], ['key2':'value2'], ...]
目标是从表单的数组中生成键/值对 [['key1':'value1'], ['key2':'value2'], ...]
Not that I really need it a lib for that: I just like to re-use things..
并不是说我真的需要它一个库:我只是喜欢重复使用东西..
回答by Peter Majeed
I think you're looking for _.object
/_.zipObject
.
我认为您正在寻找_.object
/ _.zipObject
。
http://lodash.com/docs#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);
回答by Ricardo Stuven
Since version 4, _.fromPairs
从第 4 版开始, _.fromPairs
The inverse of
_.toPairs
; this method returns an object composed from key-value pairs.
的倒数
_.toPairs
;此方法返回由键值对组成的对象。