我一直无法弄清楚如何转换嵌套数组,例如:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="foo">Details</a>
<div id="text">This is a test message for the text.</div>
进入
等对象var array = [
[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', 'henry@hardcore.net'], ['weight', 180], ['occupation', 'repo']],
[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', 'IamB@man.com'],
['weight', 200], ['occupation', 'enforcement']]
];
以下是我到目前为止所提出的内容,但显然没有产生我需要的结果。
var array = [
{firstName: 'Henry', codeName: 'Etta', email: 'henry@hardcore.net', weight: 180, occupation: 'repo'},
{firstName: 'Bruce', codeName: 'DK', email: 'IamB@man.com', weight: 200, occupation: 'enforcement'}
];
这似乎是一个本来会被问到的问题,但是几个小时之后我一直无法找到类似的问题,所以我很感激这里的任何帮助或指导。谢谢!
答案 0 :(得分:2)
您可以将数组缩减为对象
var array = [
[['firstName', 'Henry'],['codeName', 'Etta'],['email', 'henry@hardcore.net'],['weight', 180],['occupation', 'repo']],
[['firstName', 'Bruce'],['codeName', 'DK'],['email', 'IamB@man.com'],['weight', 200],['occupation', 'enforcement']]
];
var obj = array.map( arr => arr.reduce( (acc, curr) => {
acc[ curr[0] ] = curr[1]; return acc;
}, {}));
console.log(obj)
.as-console-wrapper {top:0; max-height: 100%!important}
答案 1 :(得分:1)
您可以使用展开式map()
和...
Object.assign()
var array = [[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', 'henry@hardcore.net'], ['weight', 180], ['occupation', 'repo']],[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', 'IamB@man.com'],['weight', 200], ['occupation', 'enforcement']]];
var result = array.map(e => Object.assign({}, ...e.map(([k, v]) => ({[k]: v}))))
console.log(result)
&#13;
您还可以使用map()
,然后reduce()
使用Object.assign()
var array = [[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', 'henry@hardcore.net'], ['weight', 180], ['occupation', 'repo']],[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', 'IamB@man.com'],['weight', 200], ['occupation', 'enforcement']]];
var result = array.map(e => e.reduce((r, [k, v]) => Object.assign(r, {[k]: v}),{}))
console.log(result)
&#13;
答案 2 :(得分:1)
您可以将.map()
与.reduce()
结合使用,如下所示:
var array = [
[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', 'henry@hardcore.net'], ['weight', 180], ['occupation', 'repo']],
[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', 'IamB@man.com'],
['weight', 200], ['occupation', 'enforcement']]
];
var objs = array.map(function (arr) {
return arr.reduce(function (res, curr) {
var [key, value] = curr;
res[key] = value;
return res;
}, {});
});
console.log(objs);
答案 3 :(得分:1)
您可以使用reduce
方法,它允许将数组缩减为单个变量,在本例中为对象。
有关更多信息,请查看MDN
var initialArray = [
[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', 'henry@hardcore.net'], ['weight', 180], ['occupation', 'repo']],
[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', 'IamB@man.com'],
['weight', 200], ['occupation', 'enforcement']]
];
function arrayToObject(a) {
return a.reduce(function (obj, keyValue) {
obj[keyValue[0]] = keyValue[1];
return obj;
}, {});
}
var finalArray = initialArray.map(arrayToObject);
console.log(finalArray);
答案 4 :(得分:0)
希望这个简单的逻辑可能会有所帮助。
var array = [
[
['firstName', 'Henry'],
['codeName', 'Etta'],
['email', 'henry@hardcore.net'],
['weight', 180],
['occupation', 'repo']
],
[
['firstName', 'Bruce'],
['codeName', 'DK'],
['email', 'IamB@man.com'],
['weight', 200],
['occupation', 'enforcement']
]
];
var output = [];
array.forEach(function(arr1) {
output.push({});
arr1.forEach(function(arr2) {
output[output.length - 1][arr2[0]] = arr2[1];
})
});
console.log(output);
答案 5 :(得分:0)
array.map(val => val.reduce(function(obj, prop) {
obj[prop[0]] = prop[1];
return obj;
}, {}));