PricingBlog

Count number of times a name is in a array of objects

  • ksele_-1207467339985981500

    soni

    1 year ago

    I'm trying to understand how to work with the formula editor, have some problems to convert from "normal" Javascript to the formula editor.

    If i have a list of names in an array of object like this:

    [
    { name: "John" },
    { name: "Jane" },
    { name: "John" },
    { name: "Doe" },
    { name: "Jane" },
    { name: "Doe" }
    ];

    How can i convert that to this in Toddle:

    [
    { name: 'John', count: 2 },
    { name: 'Jane', count: 2 },
    { name: 'Doe', count: 2 }
    ]

    I guess it's reduce and map, but how 😉
  • andreasmoller-1207472512330506330

    Andreas Møller

    1 year ago

    Reduce would be the most efficient way, but Filter followed by size might be simpler.
  • ksele_-1207701227719233576

    soni

    1 year ago

    Thnx for the quick response!
    If you use filter, will you not then need to manually filter by each name, i want do do it dynamically, so if there is a new name added to the list, i want to make me a new array with the count.
  • andreasmoller-1207701572231110656

    Andreas Møller

    1 year ago

    Ahh so you want the count for each name?
  • ksele_-1207723195461795980

    soni

    1 year ago

    Yes. l’m trying to make a dashboard for my accountingfirm, where i send in data on finished jobs, and want to count number each employee has finished 😉 so there is 12 employees
  • andreasmoller-1207724138114850878

    Andreas Møller

    1 year ago

    Then reduce makes sense
  • You can also use Group by
  • lucasg-1207766178274213918

    Lucas G

    1 year ago

    1207766178072625152-image.png
    💯1
  • ksele_-1207825557274886267

    soni

    1 year ago

    Thank you so much Lucas!! Worked like a charm!
    👍1