I have two arrays. First array is an array of object with each object respresenting a vote for an item, the id represents the item that was voted.
The second array contains all the options for that poll.
I want to create a new array with each option from the poll options with a new attribute having the percentage of votes they got from the votes array.
This is the votes array.
votes = [{
vote_id: 1, person: {name: ‘alan’}
}, {
vote_id: 2, person: {name: ‘John’}
},{
vote_id: 1, person: {name: ‘khan’}
}, {
vote_id: 1, person: {name: ‘martin’}
},{
vote_id: 3, person: {name: ‘mike’}
}]
Options = [{
id: 1, title: ’sweet’}, {
id: 2: ’salty’}, {
id: 3, title: ’spicy’}, {
id: 4, title: ’bitter’}]
This is the new array that I want to create from the data available from the above two arrays
new array = [{
Id: 1, title: ’sugar’, percentage: 60%},
{Id: 2, title: ’salt’, percentage: 20% },
{id: 3, title: ’spice’, percentage: 20%},
{id: 4, title: ‘bitter’, percentage: 0%}]