我从google获得数据的格式如下,我想要转换成另一种格式。
[
[ 'Teresa', 'lname', 44, 'hindi', 'math', 'sci' ],
[ 'Conn', 'de', 55, 'hindi', 'math', 'che' ],
[ 'Caterina', 'ddd', 33, 'math', 'hindi', 'bio' ],
[ 'Papagena', 'dd', 42, 'math', 'hindi', 'geo' ],
[ 'Fabien', 'des', 33, 'hindi', 'eng', '' ]
]
[
{name:'Teresa', lastName:'lname', age: 44, subjects:['hindi', 'math', 'sci' ]},
{name:'Conn', lastName:'de', age:55, subjects:['hindi', 'math', 'che' ]},
{name:'Caterina', lastName:'ddd', age:33, subjects:['math', 'hindi', 'bio' ]},
{name:'Papagena', lastName:'dd', age:42, subjects:['math', 'hindi', 'geo' ]},
{name:'Fabien', lastName:'des', age:33, subjects:['hindi', 'eng', '' ]}
]我正在努力学习,但做不到。请帮帮我..。
发布于 2022-10-16 14:16:09
解决方案
const data = [
["Teresa", "lname", 44, "hindi", "math", "sci"],
["Conn", "de", 55, "hindi", "math", "che"],
["Caterina", "ddd", 33, "math", "hindi", "bio"],
["Papagena", "dd", 42, "math", "hindi", "geo"],
["Fabien", "des", 33, "hindi", "eng", ""]
];
const result = data.reduce((acc, cur) => {
const obj = {};
obj.name = cur[0];
obj.lastName = cur[1];
obj.age = cur[2];
obj.subjects = cur.slice(3);
acc.push(obj);
return acc;
}, []);https://stackoverflow.com/questions/74087660
复制相似问题