Filter Map and Reduce

Asked

Viewed 81 times

0

I have the following line of code:

$scope.listDeColaboradoresObject.forEach(item => {
  item.listNmAssunto = 
  $scope.relatorioTotalMensagensRespondidasColab
  .filter(x => x.nmUsuario == item.nmUsuario)
  .map(x => x.nmAssunto);
    item.listNmAssunto = $scope.removeDuplicates(item.listNmAssunto);

    item.listDtResposta = $scope.relatorioTotalMensagensRespondidasColab
    .filter(x => x.nmUsuario == item.nmUsuario)
    .map(x => x.dtResposta);

});

who walks this array:

0: {deTipoAtendimento: "012", nmAssunto: "Cartão extraviado", nmUsuario: "15", dtResposta: "2018", total: 1}
1: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Admin", dtResposta: "2018", total: 2}
2: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Administrador", dtResposta: "2018", total: 1}
3: {deTipoAtendimento: "012", nmAssunto: "Assunto Novo 012", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 3}
4: {deTipoAtendimento: "012", nmAssunto: "Assunto Teste GREAt", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 2}
5: {deTipoAtendimento: "012", nmAssunto: "Thais 23042018", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 2}
6: {deTipoAtendimento: "012", nmAssunto: "teste Alterado2", nmUsuario: "Administrador IMB", dtResposta: "2018", total: 1}

and that returns to me the following array:

0: {nmUsuario: "15", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2975"}
1: {nmUsuario: "Admin", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2976"}
2: {nmUsuario: "Administrador", listNmAssunto: Array(1), listDtResposta: Array(1), $$hashKey: "object:2977"}
3: {nmUsuario: "Administrador IMB", listNmAssunto: Array(4), listDtResposta: Array(4), $$hashKey: "object:2978"}

My question is how do I make it at the time of map i insert both nmAssunto and dtResposta into the same *array

1 answer

5


Just create the object with the two properties and return it in the map callback

$scope.listDeColaboradoresObject.forEach(item => {

  item.listAssuntoResposta = $scope.relatorioTotalMensagensRespondidasColab
    .filter(x => x.nmUsuario == item.nmUsuario)
    .map(x => ({ nmAssunto: x.nmAssunto, dtResposta: x.dtResposta}) );

});

As you are using an Arrow Function, the statement stated shortly after the => is automatically interpreted as the function return. However, since you are declaring an object as return, it is important to leave it in parentheses, or the compiler/interpreter will interpret the keys as the beginning of the function’s scope, not as the beginning of an object’s declaration.

Browser other questions tagged

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