首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换数组中的Excel工作表数据

转换数组中的Excel工作表数据
EN

Stack Overflow用户
提问于 2022-10-16 13:49:18
回答 1查看 25关注 0票数 -1

我从google获得数据的格式如下,我想要转换成另一种格式。

代码语言:javascript
复制
[ 
  [ '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', '' ]}
]

我正在努力学习,但做不到。请帮帮我..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-16 14:16:09

解决方案

代码语言:javascript
复制
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;
}, []);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74087660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档