1

In a database i have two colums: id and value. I want to check how many difrent id's there is in my result, how can i do that?

I have tried to think of an solution, but i haven't succeed more that countDifferentId(){// do some magic calculations}

result looks like this:

[ RowDataPacket {
    id: 76561198115203520,
    value: 73 },
  RowDataPacket {
    id: 76561198115029751,
    value: 73 }
  RowDataPacket {
    id: 76561198115702984,
    value: 73 },
  RowDataPacket {
    id: 76561198115203520,
    value: 73 } ]

So the result of countDifferentId() should be 3

McMuffinDK
  • 431
  • 3
  • 13

3 Answers3

3

If you want to do the job on javascript side, here you go:

const rowDataPackets = [
    {
        id: 76561198115203520,
        value: 73
    },
    {
        id: 76561198115029751,
        value: 73
    },
    {
        id: 76561198115702984,
        value: 73
    },
    {
        id: 76561198115203520,
        value: 73
    }
];


function countDifferentId(array){
    // Object with key=id to count unique ids and value is true (or whatever)
    const uniqueIds = array.reduce((acc, data) => Object.assign(acc, {[data.id]:true}) , {});
    // Number of unique ids is the number of keys
    return Object.keys(uniqueIds).length;
}

console.log(countDifferentId(rowDataPackets));
McMuffinDK
  • 431
  • 3
  • 13
RaphaMex
  • 2,781
  • 1
  • 14
  • 30
2

Don't you want to do so in a group by query? There're lots of examples here. e.g.

https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-count-with-group-by.php

MySQL Query with count and group by

etc.

If you have only server request from API, you may want . to create a hash table like { id: count }

Alex Brazh
  • 450
  • 6
  • 12
  • I hoped for a JS solution as i think it is faster for me – McMuffinDK Feb 07 '18 at 01:39
  • JS is not necessarily faster, it's often better share work between servers. `Group by` is a good option. If there's a lot of rows to fetch and you **just want to count**, even better do a `select count(distinct id) from...` – RaphaMex Feb 07 '18 at 01:54
  • @RaphaMex the rows will not exceed 100 and i need to make a call every 1 second, is it still better? – McMuffinDK Feb 07 '18 at 02:01
  • I made a pure JS solution, if you want to give it a try and compare. 100 rows every second, that's not a problem for either solution. I was just commenting in case you had thousands of rows, better relieve your network. – RaphaMex Feb 07 '18 at 04:43
  • @McMuffin Hey, you may also try lodash library. const result = _.countBy(dataArray, 'id'); – Alex Brazh Feb 07 '18 at 07:42
  • 1
    i agree that doing this at the query level is ideal, but sometimes its not a viable option depending on project dynamics. – Brian Patterson Feb 07 '18 at 07:55
  • Yep, especially when you work with third-party API responses – Alex Brazh Feb 07 '18 at 07:57
1

function grabs all values from one array column and returns as an array.

then for loop uses an array to store counted values, checking the array each time to ensure values counted only once.

function getCol(myarray, col){ 
    var column = []; 
    for(var i=0; i<myarray.length; i++){ 
        column.push(myarray[i][col]); 
    } 
    return column; 
    }

    cols = getCol(result, 'id');
    var count = 0;
    var used = [];
    for(var i = 0; i<cols.length;i++){
         if(!used.indexOf(cols[i])){
               used.push(cols[i]);
               count++;
         }
     }
Brian Patterson
  • 1,615
  • 2
  • 15
  • 31