[英]best way to convert object with arrays to array with objects and viceversa
what is the best way to convert an object of arrays to an array of objects and vice-versa将 arrays 的 object 转换为对象数组的最佳方法是什么,反之亦然
{
category : ['a','b','c'],
title : ['e','f','g'],
code : ['z','x','v']
}
To到
[
{
category : 'a',
title : 'e',
code : 'z'
},
{
category : 'b',
title : 'f',
code : 'x'
},
{
category : 'c',
title : 'g',
code : 'v'
},
]
You could use two function for generating an array or an object. 您可以使用两个函数来生成数组或对象。 They works with 他们与
Object.keys
for getting all own property names, Object.keys
用于获取所有自己的属性名称,
Array#reduce
for iterating an array and collecting items for return, Array#reduce
用于迭代数组并收集要返回的项目,
Array#forEach
just fo itarating an array. Array#forEach
仅创建一个数组。
function getArray(object) { return Object.keys(object).reduce(function (r, k) { object[k].forEach(function (a, i) { r[i] = r[i] || {}; r[i][k] = a; }); return r; }, []); } function getObject(array) { return array.reduce(function (r, o, i) { Object.keys(o).forEach(function (k) { r[k] = r[k] || []; r[k][i] = o[k]; }); return r; }, {}); } var data = { category: ['a', 'b', 'c'], title: ['e', 'f', 'g'], code: ['z', 'x', 'v'] }; console.log(getArray(data)); console.log(getObject(getArray(data)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here is solution using reduce
method and arrow
function. 这是使用reduce
方法和arrow
功能的解决方案。
var obj={ category : ['a','b','c'], title : ['e','f','g'], code : ['z','x','v'] } var result=obj[Object.keys(obj)[0]].reduce((a,b,i)=>{ var newObj={}; Object.keys(obj).forEach(function(item){ newObj[item]=obj[item][i]; }); return a.concat(newObj); },[]); console.log(result);
You can use map()
and forEach()
您可以使用map()
和forEach()
var obj = { category : ['a','b','c'], title : ['e','f','g'], code : ['z','x','v'] } var result = Object.keys(obj).map(function(e, i) { var o = {} Object.keys(obj).forEach((a, j) => o[a] = obj[a][i]) return o }) console.log(result)
Not an answer to the original question, but thought I'd share this as I came here looking for a solution to a related problem: I needed the Cartesian product of values of uneven length.不是原始问题的答案,但我想在我来这里寻找相关问题的解决方案时分享这个:我需要不均匀长度值的笛卡尔积。 What I'm using for that:我正在使用什么:
/** * Get the Cartesian product of a list of array args. */ const product = (...args) => args.length === 1? args[0].map(x => [x]): args.reduce( (a, b) => Array.from(a).flatMap( v1 => Array.from(b).map(v2 => [v1, v2].flat()) ) ); /** * Map[String,Array[Any]] -> Array[Map[String,Any]] */ const arrayValuesToObjectArray = (obj) => product(...Object.values(obj)).map(values => Object.fromEntries( Object.keys(obj).map((k, idx) => [k, values[idx]]) )); console.log(arrayValuesToObjectArray({ hello: ["this", "is", "a", "test"] })); console.log(arrayValuesToObjectArray({ one: ["foo"], two: ["bar", "baz"], three: ["this", "is", "three"], })); console.log(arrayValuesToObjectArray({ one: "foo", two: "bar baz", three: "this is three", }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.