I have a js file that has an array of people.
data.js
const data = [ [1, '', 'Fresno', 'name 1', 'name 2', 18, 'women'], [2, '', 'San Francisco', 'name 1', 'name 2', 20, 'man'], [3, '', 'Los Angeles', 'name 1', 'name 2', 22, 'women'], [4, '', 'Amador', 'name 1', 'name 2', 23, 'man'], [5, '', 'Kern', 'name 1', 'name 2', 33, 'women'], [6, '', 'Butte', 'name 1', 'name 2', 32, 'women'], [7, '', 'Santa Barbara', 'name 1', 'name 2', 21, 'man'], ];
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.
let newData = data.map(({name, gender}) => ({ value: name, group: gender }))
Advertisement
Answer
Your data is an array so you will have to use its index.
const data = [ [1, '', 'Fresno', 'name 1', 'name 2', 18, 'women'], [2, '', 'San Francisco', 'name 1', 'name 2', 20, 'man'], [3, '', 'Los Angeles', 'name 1', 'name 2', 22, 'women'], [4, '', 'Amador', 'name 1', 'name 2', 23, 'man'], [5, '', 'Kern', 'name 1', 'name 2', 33, 'women'], [6, '', 'Butte', 'name 1', 'name 2', 32, 'women'], [7, '', 'Santa Barbara', 'name 1', 'name 2', 21, 'man'], ]; let newData = data.map((i) => ({ value: i[3], group: i[6] })) console.log(newData);