PricingBlog

Array of objects manipulation question

  • jehex-1499965825275400323

    Jehex

    13 days ago

    Hi! Quick question about array manipulation.

    I’m building an array of objects { id, commentsCount }, initially empty, that gets updated from a child component event. Each time a user comments, the event returns the post ID.

    The goal is to keep a temporary local array of posts and their comment counts to update the UI without refreshing or calling the API.

    How should I handle adding/updating objects in this array with these two cases :

    If an object with the same id exists → increment commentsCount
    If not → add { id, commentsCount: 1 } to the array

    Thanks you
    1499965826877620445-Untitled.png
  • plusmin-1500072260311060571

    Armand

    13 days ago

    You could put a switch in the commented event. If id exists go down that path (find object with that id and set new value), otherwise down the Else path and append new object.
  • jehex-1500383320561680614

    Jehex

    12 days ago

    Hello there, is it possible to show me the formula to modify one of the attribute of an object from an array where for example the id is found ( not sure Im clear lol )
  • plusmin-1500431350572908725

    Armand

    12 days ago

    If I understand your question correctly, then you want to see how to add a new commentsCount in these two scenarios:
    1. It is the first comment and no commentsCount exists yet -> append commentsCount to existing array.
    2. It is not the first comment and a commentsCount already exists -> update existing commentsCount object with existing number + 1.
    1500431350002614433-Screenshot_2026-05-03_at_16.35.24.png
    1500431350497546340-Screenshot_2026-05-03_at_16.32.59.png
  • jehex-1500458240801243187

    Jehex

    12 days ago

    Hi there, thanks for your anwser, Im gonna check it now
  • jehex-1500462699622961214

    Jehex

    12 days ago

    I think I have an issue with my condition for check if an object with the same id has been received already exist ( object who contain the id / comment count )
    1500462699329491054-Untitled.png
  • plusmin-1500466748586197013

    Armand

    12 days ago

    Try to put some data in your variable and also confirm what your event data looks like. Then setting it up and testing it will be much easier