PricingBlog

Array of objects manipulation question

  • jehex-1499965825275400323

    Jehex

    9 hours 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

    2 hours 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.