1
While using map, filter, reduce to access data an array of objects was all quiet, now I came across access a nesting so locked everything here.
const objects = [{ to: 'a', b: 'b', c: [ { cc: 1, dd: 2, ee: 3' }, { cc: 4, dd: 5, ee: 6' } ]
thought to access with map (objects.map(c => c.c.dd) to receive as result: 2 and 5 using filter and spread I can BUT, I don’t want to filter, I just want to receive the value contained in dd
EDITING ...user140828
const posts = [{
title: 'title to',
Subject: 'subject to',
vows: [
{ reader: 1, note: 4}, (of a maximum total of 5 points)
{ reader: 4, note: 5 },
{ reader: 2, note: 3 }
]
]}
What I need for each Post in the Posts array:
Vue Component needs to score from 1 to 5 a post qd the user wants to vote on the post.
the Vue Component shows the current score of the post to the reader and allows it to point if it is of your will.
total of votes: 3 votes.
accurate average of votes (to feed Vue) note 4 + 5 + 3 = 12 divided by the number of votes (3) I get the result 4 and with this result feed the component so when the user reads the post he will know that that post has already been voted 3 times and the current score is 4 points of a total of 5 so the post is in high understood?
with your code I got all this but, at the time I need to get the average (sum of votes / total of votes) is that I find subject the code.
map is not to get the length but rather the notes records that are in a post object votes.
no reduce, I’m summing up all notes and reduce result I’m dividing with total notes to get average points(votes).
with your code already solved everything and this working just think I covered the code and wanted an evaluator to fix (or not).
your code:
const dd = .map objects(object => object.c.map(c => c.dd))
I took it and with forIn I killed the first map because the component runs under forIn so it was like this:
post.votos.map(c => c.note)
with this, I have all the votes(notes 4, 5, 3) so I used
.reduce(....) to add up the votes 4+5+3=12
Now that I think I’ve made some:
to get the total vote record (in the example above are 3 records) I used:
post.votos.map(c => c.note). length) = 3, so I took the average this way:
post.votos.map(c => c.note). reduce((a, b) => a + b) / post.votos.map(c => c.note). length)
As I said, with your code I solved everything but I think I have sinned at the end and it is your opinion since it was through your code that I was able to access the property votes which is an array from within another array.
I explained it right now or confused it even more?
Sorry for your time, as I said, thanks to you it’s all working, but I believe that calling another map just to get the total note records got kind of weird. Regardless of anything I am very grateful for having asked me to use map within map.
It fell like a glove. Grateful for your help, it was of immense importance!
– sidiara castro
Based on your snippet I got this through a for in: I wanted to know if I am matching this code dividing the result of the sum by the total value of records? I put my hands to my legs? or with own reduce I could know how many iterations occurred and then split? posts.map(p => p.votos.map(c => c.int_votes)) with its code already with for in: post.votos.map(c => c.int_votes). reduce(a, b) => a + b / post.votos.map(c => c.int_votes).length - 1) works fine but I think I killed javascript not?
– sidiara castro
I don’t understand exactly what you’re trying to do. Finding the average? Because what you’re doing is splitting the result of each iteration of
reduce
by the size of your array, not the value returned from reduce as it should be. Invokemap
in an array only to find thelength
also doesn’t make any sense, the return of the map always has the same size as the array, so to invoke map?– Andre
I’ll explain in another question because here there is no space.
– sidiara castro
Boy you are a genius!!!! I knew that map was strange although it works the way I was bothered. Until today I had not understood the reduce accumulator and unwillingly, with your example I ended up unintentionally understanding the purpose of acc. Your syntax is short, pure and effective. .voilá..... fit right in. Thank you very much!
– sidiara castro