[英]How to transform object into array of objects?
I want to transform this to an array of objects ordered based on an array of keys: 我想将其转换为基于键数组排序的对象数组:
{
tom: 11,
jim: 22,
jay: 13
}
Input -> Output examples: 输入->输出示例:
['jim', 'tom', 'kim', 'jay']
-> [{jim: 22}, {tom: 11}, {jay: 13}]
['jim', 'tom', 'kim', 'jay']
-> [{jim: 22}, {tom: 11}, {jay: 13}]
['may', 'jay', 'tom', 'jim']
-> [{jay: 13}, {tom: 11}, {jim: 22}]
['may', 'jay', 'tom', 'jim']
-> [{jay: 13}, {tom: 11}, {jim: 22}]
How can I accomplish this? 我该怎么做? I'd rather a one line lodash solution.
我宁愿一个单行lodash解决方案。
For what it is worth, here's a JS function that performs what you want. 对于它的价值,这是一个执行所需功能的JS函数。
function transform(targetObj, keyArray) {
let resultArray = [];
// Sanity (may want to throw error here)
if (!targetObj || !keyArray) { return resultArray; }
for (let i = 0; i < keyArray.length; i++) {
if (!targetObj.hasOwnProperty(keyArray[i])) { continue; }
let item = {};
item[keyArray[i]] = targetObj[keyArray[i]];
resultArray.push(item);
}
return resultArray;
}
Tests 测验
let obj = { tom: 11, jim: 22, jay: 13 };
let input1 = ['jim', 'tom', 'kim', 'jay'];
let input2 = ['may', 'jay', 'tom', 'jim'];
let result1 = transform(obj, input1);
console.log(JSON.stringify(result1));
let result2 = transform(obj, input2);
console.log(JSON.stringify(result2));
Assuming the object is named obj
and the list of keys to look up is named keys
: 假设对象名为
obj
,要查找的键列表名为keys
:
_.zip([keys, keys.map(x => obj[x])]).filter(([k, v]) => v).map(x => _.fromPairs([x]))
Or, if you don't care about order, there's another way: 或者,如果您不关心订单,那么还有另一种方法:
_.toPairs(obj).filter(([k, v]) => _.include(keys, k)).map(x => _.fromPairs([x]))
A one-liner lodash solution would be to use lodash#toPairs
to convert the object into an array of key-value pairs, lodash#chunk
to wrap each pairs with another array as preparation for mapping each item to form the pairs into an object using lodash#map
with a lodash#fromPairs
iteratee. 单行lodash解决方案是使用
lodash#toPairs
将对象转换为键-值对数组, lodash#chunk
将每个对与另一个数组包装在一起,以准备使用以下各项将每个项映射为对象lodash#map
和lodash#fromPairs
iteratee。
var result = _(data).toPairs().chunk().map(_.fromPairs).value();
var data = { tom: 11, jim: 22, jay: 13 }; var result = _(data).toPairs().chunk().map(_.fromPairs).value(); console.log(result);
.as-console-wrapper{min-height:100%;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
An Vanilla JS ES6 solution would be to use Object#keys
to get an array of keys from the object, Array#map
to map each keys to associate each key to form an array of objects. Vanilla JS ES6解决方案是使用
Object#keys
从对象获取Object#keys
数组,使用Array#map
映射每个键以关联每个键以形成对象数组。 We used the ES6 computed property names feature to associate the keys directly into an anonymous object. 我们使用ES6计算属性名称功能将键直接关联到匿名对象中。
var result = Object.keys(data).map(key => ({ [key]: data[key] }));
var data = { tom: 11, jim: 22, jay: 13 }; var result = Object.keys(data).map(key => ({ [key]: data[key] })); console.log(result);
.as-console-wrapper{min-height:100%;top:0}
If the browsers that you use doesn't support ES6 then you can simply convert the solution above into this: 如果您使用的浏览器不支持ES6,则可以将上述解决方案简单地转换为:
var result = Object.keys(data).map(function(key) {
var object = {};
object[key] = data[key];
return object;
});
var data = { tom: 11, jim: 22, jay: 13 }; var result = Object.keys(data).map(function(key) { var object = {}; object[key] = data[key]; return object; }); console.log(result);
.as-console-wrapper{min-height:100%;top:0}
Basically for a one liner solution to this you don't need lodash
, just simple filter
and map
would be enough, however if you want you can use lodash
. 基本上,对于一个线性解决方案,您不需要
lodash
,只需简单的filter
和map
就足够了,但是,如果您愿意,可以使用lodash
。
//this is your source object
var keys = {
tom: 11,
jim: 22,
jay: 13
}
Now, Lets assume you have 2 (some) arrays 现在,假设您有2个(一些)数组
var input1 = ['jim', 'tom', 'kim', 'jay'];
var input2 = ['may', 'jay', 'tom', 'jim'];
And here we go: 现在我们开始:
//for first input
input1.filter(s=>keys[s]).map(s=> ({[s]: keys[s]}));
//for secondinput
input2.filter(s=>keys[s]).map(s=> ({[s]: keys[s]}));
Here is a working snippet for you: 这是适合您的代码段:
var keys = { tom: 11, jim: 22, jay: 13 } var input1 = ['jim', 'tom', 'kim', 'jay']; var input2 = ['may', 'jay', 'tom', 'jim']; var output1 = input1.filter(s=>keys[s]).map(s=> ({[s]: keys[s]})); var output2 = input2.filter(s=>keys [s]).map(s=> ({[s]: keys[s]})); console.log("Output1: ", JSON.stringify(output1)); console.log("Output2: ", JSON.stringify(output2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.