Accessing a property of an object array within another object array

Asked

Viewed 735 times

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.

2 answers

2


You have an array within an array, so to isolate these properties, you will need a map inside a map.

const objetos = [{ a: 'a', b: 'b', c: [ { cc: 1, dd: 2, ee: 3 }, { cc: 4, dd: 5, ee: 6 } ] }]

const dd = objetos.map(objeto => objeto.c.map(c => c.dd))
//dd contém uma matriz, com as propriedades dd de todos os objetos
console.log(dd)
//como no exemplo só existe um objeto na raiz, você pode acessa-lo diretamente no índice 0
console.log(dd[0])

To find the votes averages on the other hand, you don’t necessarily need to use the map method, you can directly call the reduce and pass not only the callback, but also the initial value for the accumulator (in this case 0).

const posts = [{ titulo: 'titulo a', assunto: 'assunto a', votos: [{ leitor: 1, nota: 4 }, { leitor: 4, nota: 5 }, { leitor: 2, nota: 3 }] }]

//Encontrando notas, e então as médias
const notas = posts.map(post => post.votos.map(voto => voto.nota))
const medias1 = notas.map(nota => nota.reduce((a, b) => a + b) / nota.length)

//Encontrando médias diretamente
const medias2 = posts.map(post => post.votos.reduce((acc, voto) => acc + voto.nota, 0) / post.votos.length)

console.log(medias1)
console.log(medias2)

  • It fell like a glove. Grateful for your help, it was of immense importance!

  • 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?

  • 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. Invoke map in an array only to find the length also doesn’t make any sense, the return of the map always has the same size as the array, so to invoke map?

  • I’ll explain in another question because here there is no space.

  • 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!

0

You can also extract the properties by combining array.from and destructing. For example:

const users = [
  { name: 'John' , 
    active: false }, 
  { name: 'Mary' , 
    active: true }, 
];

const userNames = Array.from(users, ({ name }) => name);
  • Show!!!! is the es breaking the "almost impossible" and you masters showing us the way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.