Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to group multiple arrays into one data structure

R.IJK

New Coder
I am a beginner to Javascript and struggling a lot with structuring and displaying my data. I have 4 arrays:
JavaScript:
var iDs = [1234, 5678]
var sources = [[ABC, DEF, GHI], [ABC, DEF, GHI]]
var messages = [[[a, a,], [b ,b ], [c ,c]], [[aa, aa,], [bb ,bb ], [cc ,cc]]]
var time = [[[123, 456,], [789 ,101112 ], [1314 ,1516]], [[112, 223,], [563 ,657 ], [890 ,7869]]]

iDs[0] is the id of source[0]. messages [0] represent the messages of source [0] and time [0] represent the time of messages[0]. For e.g. [a,a] is the message from ABC with time [123, 456], [b,b] is the message from DEF with time [789, 101112] and so on.

This is clearly not a good way to link these arrays. How can I best group and link these arrays into one data structure?
 
Last edited:
Hello,

You should look into classes. Create a class that contains the id, source, time and content of the message and make an array of objects of that class.
The id, source, time and content are coming from a subscription which is in a class. I parse the data received through that subscription an store it in these individual arrays. How would another class help me in with this?
 
The id, source, time and content are coming from a subscription which is in a class. I parse the data received through that subscription an store it in these individual arrays. How would another class help me in with this?
Why would you extract them into individual arrays?
 
Why would you extract them into individual arrays?
I am using those arrays to display the sources and messages like this on the UI: 1615741408915.png
A, B, C, D are sources from which each message is coming from. When the user hovers on the message it displays it's time. I am using *ngFor from Angular to achieve this. Is there a better to do this without using individual arrays?
 
I do not know Angular.

I do not understand? Simply retrieve the message id from the hovered message and then retrieve the object associated with it. Once you have the object, you can simply use its methods to retrieve the information about the message in order to display them.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom