-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path32.js
58 lines (51 loc) · 1.29 KB
/
32.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
从某数据库接口得到如下值:
{
rows: [
["Lisa", 16, "Female", "2000-12-01"],
["Bob", 22, "Male", "1996-01-21"]
],
metaData: [
{ name: "name", note: '' },
{ name: "age", note: '' },
{ name: "gender", note: '' },
{ name: "birthday", note: '' }
]
}
rows 是数据,metaData 是对数据的说明。现写一个函数 parseData,将上面的对象转化为期望的数组:
[
{ name: "Lisa", age: 16, gender: "Female", birthday: "2000-12-01" },
{ name: "Bob", age: 22, gender: "Male", birthday: "1996-01-21" },
]
*/
const parseData = (data) => {
// 循环rows.length次
const { rows, metaData } = data;
const columns = metaData.map(v => v.name);
return rows.map((v, i) => {
let parseObj = {};
v.map((v, i) => {
parseObj = {
...parseObj,
[columns[i]]: v,
}
});
return parseObj;
});
}
const data = {
rows: [
["Lisa", 16, "Female", "2000-12-01"],
["Bob", 22, "Male", "1996-01-21"]
],
metaData: [
{ name: "name", note: '' },
{ name: "age", note: '' },
{ name: "gender", note: '' },
{ name: "birthday", note: '' }
]
};
console.log(parseData(data));
const parseData = (data) => data.rows.map(x => {
return data.metaData.reduce((acc, meta, i) => ({ ...acc, [meta.name]: x[i] }) , {})
})