How to transform a matrix of a SINGLE object?

Asked

Viewed 54 times

-1

My question is this, how can I make a matrix that has 3 objects become a single object ? According to the print below, the matrix is bringing me 3 objects, however I need to assemble a single object with the value of each matrix, I have researched something like reduce, but I could not achieve success. Here is my code :

       //"ITEMS" é um FirebaseListObservable que transformo em um array
       //javascript

       //dadosPontoFuncionario é o meu array javascript, que está retornando
       //o que está no console, é ele que preciso que vire um único objeto

       this.items.subscribe( data => {
         this.dadosPontoFuncionario = data;
         console.log(this.dadosPontoFuncionario);
       })

inserir a descrição da imagem aqui

  • What array? you don’t have an object array?

  • How you will treat if there are two identical keys when joining objects in your array?

  • I have an array of objects, but I would like to turn it into a single object you understand, unite the 3 objects into one, I don’t even know if it has to do this. It depends on which key he considers, the $key is unique, but I have two other types of keys that are repeated on each object of that.

  • I still can’t understand... you generate a single object consolidating all the properties of all objects in the array? or create a replica on a single object where each object would be a property? Ex.: { item1: {}, item2:{} ...}

  • Generate a single object by consolidating all properties of all objects in the array. For you to understand better, it is a point registration application. So each object corresponds to a recorded point, I have the starting point, interval point, end of hours point. I need to show them on the screen for the user, but to organize better, I wanted to join the 3 objects, so I can have access to information of the 3 at the same time and I can show this information on the screen. If not I have to show the separate objects in the view.

1 answer

0

I don’t know if I got it right, but I’ll try...

If that’s what I’m imagining,

const arrayObject = [
{nome:'Objeto um'},
{teste:'Objeto dois'},
{nome:'Objeto tres'}
]

console.log(arrayObject.reduce((a,b)=>({...a, ...b})))

you can do a reduce, the problem is that if the objects have keys with the same name will be worth the value of the key of the last object found..

  • Exactly, I got the same result as your code. But there’s this key problem. In that listObservable, I have a field that is repeated in the 3 objects, because I need it for database query. I guess I’ll just have to find another way...

  • Because the company and the companyKey I use to compare whether the employee belongs to that company or not, since it is a multi-user application, I will have to see another way to do I think. But I’m grateful for your answer

Browser other questions tagged

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