currently I have an array of people
const members = [{
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [0, 1, 2, 4, 5]
}, {
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [0, 1, 2, 3, 5]
}, {
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [4, 6, 7, 8]
}];
As you can see the focus of the members is holding an array. I use this array as a list of keys for another array (behaving like a dictionary) containing skills.
const skills = [
'Skill 0',
'Skill 1',
'Skill 2',
'Skill 3'
];
Before using my members array I want to replace this focus array with a string containing all the skills.
I want to map each focus item to a string holding the skill. The I want to convert this string array to a single string and add space after commas.
The result would be focus: 'Skill 1, Skill 3, Skill 7'
I went for this code
const members = [{
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [0, 2, 3]
}, {
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [0, 1, 2]
}, {
imgUrl: 'resources/members/xxx.png',
name: 'xxx',
focus: [1, 3]
}];
const skills = [
'Skill 0',
'Skill 1',
'Skill 2',
'Skill 3'
];
const targetMembers = members.map(member =>
member.focus.map(skillIndex => skills[skillIndex])
.toString()
.split(',')
.join(', ')
);
console.log(targetMembers);
but the other member attributes get lost. What is wrong with the mapping, how can I fix it?