I have a js file that has an array of people.
data.js
JavaScript
x
10
10
1
const data = [
2
[1, '', 'Fresno', 'name 1', 'name 2', 18, 'women'],
3
[2, '', 'San Francisco', 'name 1', 'name 2', 20, 'man'],
4
[3, '', 'Los Angeles', 'name 1', 'name 2', 22, 'women'],
5
[4, '', 'Amador', 'name 1', 'name 2', 23, 'man'],
6
[5, '', 'Kern', 'name 1', 'name 2', 33, 'women'],
7
[6, '', 'Butte', 'name 1', 'name 2', 32, 'women'],
8
[7, '', 'Santa Barbara', 'name 1', 'name 2', 21, 'man'],
9
];
10
In another file, I want to get data from an array and form my array with name, gender data.
I use the map, but I can’t figure out how to implement it.
JavaScript
1
5
1
let newData = data.map(({name, gender}) => ({
2
value: name,
3
group: gender
4
}))
5
Advertisement
Answer
Your data is an array so you will have to use its index.
JavaScript
1
16
16
1
const data = [
2
[1, '', 'Fresno', 'name 1', 'name 2', 18, 'women'],
3
[2, '', 'San Francisco', 'name 1', 'name 2', 20, 'man'],
4
[3, '', 'Los Angeles', 'name 1', 'name 2', 22, 'women'],
5
[4, '', 'Amador', 'name 1', 'name 2', 23, 'man'],
6
[5, '', 'Kern', 'name 1', 'name 2', 33, 'women'],
7
[6, '', 'Butte', 'name 1', 'name 2', 32, 'women'],
8
[7, '', 'Santa Barbara', 'name 1', 'name 2', 21, 'man'],
9
];
10
11
let newData = data.map((i) => ({
12
value: i[3],
13
group: i[6]
14
}))
15
16
console.log(newData);