在普通JS中以递归方式对对象的数组 [英] Array of Arrays to Object RECURSIVELY in plain JS
问题描述
在纯JS(无库)中解决数组到对象(数组内)的任何递归方法。基本上,我想将相同的函数发送给自己。这时,我用上一次迭代覆盖了该对象。
i得到:
{firstName: Sallito,lastName: Jordan,年龄:16,角色:服务器}
而我应该得到:
[
{firstName:'Eren',lastName:'Duran',年龄:22,角色:'admin'},
{firstName:'Sallito' ,lastName:'Jordan',年龄:16,角色:'server'}
]
原始数组为:
var数组= [
[
['firstName','Eren '],['lastName','Duran'],['age',22],['role','admin']
],
[
['firstName', 'sallito'],['lastName','Jordan'],['age',16],['role','server']
]
];
我目前的解决方案是:
函数transformData(array){
var obj = {};
for(var i = 0; i< array.length; i ++){
for(var j = 0; j< array [i] .length; j ++){
for (var k = 0; k< array [i] [j] .length; k ++){
if(k === 0){
var currentKey = array [i] [j] [ k];
var currentValue = array [i] [j] [k +1];
obj [currentKey] = currentValue;
}
}
}
}
return obj;
}
请只提供递归的想法,因为我知道如何使用forEach和map减少方法。我真的很想学习递归性。谢谢!
使用纯JS,您可以执行以下操作;
var data = [[['firstName','Eren'],['lastName','Duran'],['age' ,22],['role','admin']],[['firstName','Sallito'],['lastName','Jordan'],['age',16],['role','服务器']]],objData = data.map(a => a.reduce((o,t)=> Object.assign(o,{[t [0]]:t [1]}),{ })); console.log(objData);
说明:我们在主数组上应用 .map()
,它的子数组在其回调函数之一中分配给 a
自变量每转一圈。因此,在第一回合中 a
是;
[['firstName' ,'Eren'],
['lastName','Duran'],
['age',22],
['role','admin']],
现在, a
是一个带有子数组项的数组(元组-值对),我们想加入一个对象。因此,将 .reduce()
应用于 a
对该工作非常方便;
a.reduce((o,t)=> Object.assign(o,{[t [0]]:t [1]}),{} )
我们的 .reduce()
函数是带有初始值的类型,它是上述代码的最后一部分 ...,{})
。此最初为空的对象将分配给其回调的第一个参数 o
。然后,每个像 ['firstName','Eren']
这样的元组将被分配给回调的第二个参数 t
每转一圈。借助于 Object.assign(o,{[t [0]]:t [1]})
指令将它们累积在空对象中。它会使用当前的 o
空值,并将 {firstname: Eren}
合并(加入) 。箭头函数将返回 Object.assign()
的输出,作为新的 o
值,以供下一轮使用。在下一回合中, o
现在为 {名字: Eren}
和 t
是 [ lastname, Duran]
,如果我们替换这些值,我们现在有了 Object.assign({firstname: Eren},{姓氏: Duran})
,它将返回 {名字: Eren,姓氏: Duran}
和这将作为新的 o
返回,依此类推...
所以我希望现在已经清楚了。 / p>
Any Recursive-approach solutions on solving array to object/s (within array) in plain JS (no libraries). Basically I would want to send the same function to itself. At this time I get overwritten the object with the last iteration. i get:
{firstName: "Sallito", lastName: "Jordan", age: 16, role: "server"}
while i should get:
[
{firstName: 'Eren', lastName: 'Duran', age: 22, role: 'admin'},
{firstName: 'Sallito', lastName: 'Jordan', age: 16, role: 'server'}
]
The original array is:
var array = [
[
['firstName', 'Eren'], ['lastName', 'Duran'], ['age', 22], ['role', 'admin']
],
[
['firstName', 'Sallito'], ['lastName', 'Jordan'], ['age', 16], ['role', 'server']
]
];
my current so-far solution is:
function transformData(array) {
var obj = {};
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array[i].length; j++){
for(var k = 0; k < array[i][j].length; k++){
if(k === 0){
var currentKey = array[i][j][k];
var currentValue = array[i][j][k + 1];
obj[currentKey] = currentValue;
}
}
}
}
return obj;
}
Please offer just recursive ideas since I know how to do it with forEach and map.reduce ways. I really want to learn recursive-ity. Thanks!
With pure JS you may do as follows;
var data = [ [ ['firstName', 'Eren'],
['lastName', 'Duran'],
['age', 22],
['role', 'admin'] ],
[ ['firstName', 'Sallito'],
['lastName', 'Jordan'],
['age', 16],
['role', 'server'] ]
],
objData = data.map(a => a.reduce((o,t) => Object.assign(o,{[t[0]]:t[1]}),{}));
console.log(objData);
Explanation: We are applying .map()
over the main array and it's sub arrays are assigned to a
argument in it's callback one by one for each turn. So in the first turn a
is;
[ ['firstName', 'Eren'],
['lastName', 'Duran'],
['age', 22],
['role', 'admin'] ],
Now, a
is an array with sub array items (tuples - value pairs) which we would like to join in an object. So applying .reduce()
to a
comes very handy for this job;
a.reduce((o,t) => Object.assign(o,{[t[0]]:t[1]}),{})
Our .reduce()
function is the type which takes an initial value which is the last part of the above code, ...,{})
. This initially empty object will be assigned to it's callback's first argument o
. Then every tuple like ['firstName', 'Eren']
will be assigned to the callback's second argument t
one by one at each turn. They will be accumulated in the empty object by the help of Object.assign(o,{[t[0]]:t[1]})
instruction. It will take the current empty value of o
and will merge (join) {firstname: "Eren"}
to it. Arrow function will return the output of Object.assign()
as the new o
value to be used in the next turn. In the next turn o
is now {firstname: "Eren"}
and t
is ["lastname", "Duran"]
and if we substitute the values we now have Object.assign({firstname: "Eren"},{lastname: "Duran"})
which will return {firstname: "Eren", lastname: "Duran"}
and this will be returned as our new o
and so forth...
So i hope it's clear now.
这篇关于在普通JS中以递归方式对对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!