1
        data = [
        {
            "index": 0,
            "id": 47,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 3.8,
            "unnamed:_3": 1.6,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 1,
            "id": 48,
            "sepallengthcm": 4.6,
            "sepalwidthcm": 3.2,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 2,
            "id": 49,
            "sepallengthcm": 5.3,
            "sepalwidthcm": 3.7,
            "unnamed:_3": 1.5,
            "petalwidthcm": 0.2,
            "species": "jennifer"
        },
        {
            "index": 3,
            "id": 50,
            "sepallengthcm": 5.0,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 4,
            "id": 97,
            "sepallengthcm": 12.0,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.2,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 5,
            "id": 98,
            "sepallengthcm": 6.2,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.3,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 6,
            "id": 99,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 2.5,
            "unnamed:_3": 3.0,
            "petalwidthcm": 1.1,
            "species": "kajol"
        },
        {
            "index": 7,
            "id": 100,
            "sepallengthcm": 11.0,
            "sepalwidthcm": 2.8,
            "unnamed:_3": 7.0,
            "petalwidthcm": 1.3,
            "species": "floaw"
        },
        {
            "index": 8,
            "id": 101,
            "sepallengthcm": 6.3,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 6.0,
            "petalwidthcm": 2.5,
            "species": "Iris-flower"
        },
        {
            "index": 9,
            "id": 102,
            "sepallengthcm": 5.8,
            "sepalwidthcm": 2.7,
            "unnamed:_3": 5.1,
            "petalwidthcm": 1.9,
            "species": "Iris-flower"
        }
        ]

Here is my input data. I am trying to achive distict count of this data using spcific field

result = distictCount("species")

    result = [
            {
            "species": "Iris-flower",
            "sepallengthcm": 2
            },
            {
            "species": "floaw",
            "sepallengthcm": 1
            },
            {
            "species": "jennifer",
            "sepallengthcm": 3
            },
            {
            "species": "kajol",
            "sepallengthcm": 1
            },
            {
            "species": "setosa",
            "sepallengthcm": 3
            }
        ]


SELECT species, COUNT(DISTINCT sepallengthcm) as sepallengthcm FROM soubhagyairis GROUP BY species;

My sql query does the work. But, i wants to achive this using javascript. Please take a look.

How can we do that

Thanks

My sql query does the work. But, i wants to achive this using javascript. Please take a look.

How can we do that

Thanks

3 Answers 3

2

You can do this using for loop:

Snippet:

var data = [
        {
            "index": 0,
            "id": 47,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 3.8,
            "unnamed:_3": 1.6,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 1,
            "id": 48,
            "sepallengthcm": 4.6,
            "sepalwidthcm": 3.2,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 2,
            "id": 49,
            "sepallengthcm": 5.3,
            "sepalwidthcm": 3.7,
            "unnamed:_3": 1.5,
            "petalwidthcm": 0.2,
            "species": "jennifer"
        },
        {
            "index": 3,
            "id": 50,
            "sepallengthcm": 5.0,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 4,
            "id": 97,
            "sepallengthcm": 12.0,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.2,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 5,
            "id": 98,
            "sepallengthcm": 6.2,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.3,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 6,
            "id": 99,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 2.5,
            "unnamed:_3": 3.0,
            "petalwidthcm": 1.1,
            "species": "kajol"
        },
        {
            "index": 7,
            "id": 100,
            "sepallengthcm": 11.0,
            "sepalwidthcm": 2.8,
            "unnamed:_3": 7.0,
            "petalwidthcm": 1.3,
            "species": "floaw"
        },
        {
            "index": 8,
            "id": 101,
            "sepallengthcm": 6.3,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 6.0,
            "petalwidthcm": 2.5,
            "species": "Iris-flower"
        },
        {
            "index": 9,
            "id": 102,
            "sepallengthcm": 5.8,
            "sepalwidthcm": 2.7,
            "unnamed:_3": 5.1,
            "petalwidthcm": 1.9,
            "species": "Iris-flower"
        }
        ];
        
var result = [];
var intermediate = [];

for (var i = 0; i < data.length; i++) {
    intermediate[i] = data[i].species;
}
intermediate.sort();
var n = 0;
for (var i = 0; i <= intermediate.length; i++) {
    
    if (intermediate[i] !== intermediate[i - 1]) {
        if (intermediate[i - 1] !== undefined) {
            result.push({"flower": intermediate[i-1], "n": n});
        }
        n = 1;
    } else {
        n++;
    }
}
console.log(result);

Code:

var result = [];
var intermediate = [];

for (var i = 0; i < data.length; i++) {
    intermediate[i] = data[i].species;
}
intermediate.sort();
var n = 0;
for (var i = 0; i <= intermediate.length; i++) {
    
    if (intermediate[i] !== intermediate[i - 1]) {
        if (intermediate[i - 1] !== undefined) {
            result.push({"flower": intermediate[i-1], "n": n});
        }
        n = 1;
    } else {
        n++;
    }
}
console.log(result);
Sign up to request clarification or add additional context in comments.

1 Comment

It is giving me as a list
1

You can try this:

const distinctCount = (data, group, key) => {
    return Array.from(new Set(data.map(d => d[group])))
      .map(g => {return {
        [group]: g,
        [key]: data.filter(d => d[group] == g).length
  }});
}

Using new Set(data.map(d => d[group])) makes a Set which does the hard work of SQL DISTINCT for you and using Array.from(etc) converts to a regular Javascript Array.

The .map(...) then creates a new object for each DISTINCT species and finds the count using data.filter(d => d[group] == g).length

data = [
  {
    "index": 0,
    "id": 47,
    "sepallengthcm": 5.1,
    "sepalwidthcm": 3.8,
    "unnamed:_3": 1.6,
    "petalwidthcm": 0.2,
    "species": "setosa"
  },
  {
    "index": 1,
    "id": 48,
    "sepallengthcm": 4.6,
    "sepalwidthcm": 3.2,
    "unnamed:_3": 1.4,
    "petalwidthcm": 0.2,
    "species": "setosa"
  },
  {
    "index": 2,
    "id": 49,
    "sepallengthcm": 5.3,
    "sepalwidthcm": 3.7,
    "unnamed:_3": 1.5,
    "petalwidthcm": 0.2,
    "species": "jennifer"
  },
  {
    "index": 3,
    "id": 50,
    "sepallengthcm": 5.0,
    "sepalwidthcm": 3.3,
    "unnamed:_3": 1.4,
    "petalwidthcm": 0.2,
    "species": "setosa"
  },
  {
    "index": 4,
    "id": 97,
    "sepallengthcm": 12.0,
    "sepalwidthcm": 2.9,
    "unnamed:_3": 4.2,
    "petalwidthcm": 1.3,
    "species": "jennifer"
  },
  {
    "index": 5,
    "id": 98,
    "sepallengthcm": 6.2,
    "sepalwidthcm": 2.9,
    "unnamed:_3": 4.3,
    "petalwidthcm": 1.3,
    "species": "jennifer"
  },
  {
    "index": 6,
    "id": 99,
    "sepallengthcm": 5.1,
    "sepalwidthcm": 2.5,
    "unnamed:_3": 3.0,
    "petalwidthcm": 1.1,
    "species": "kajol"
  },
  {
    "index": 7,
    "id": 100,
    "sepallengthcm": 11.0,
    "sepalwidthcm": 2.8,
    "unnamed:_3": 7.0,
    "petalwidthcm": 1.3,
    "species": "floaw"
  },
  {
    "index": 8,
    "id": 101,
    "sepallengthcm": 6.3,
    "sepalwidthcm": 3.3,
    "unnamed:_3": 6.0,
    "petalwidthcm": 2.5,
    "species": "Iris-flower"
  },
  {
    "index": 9,
    "id": 102,
    "sepallengthcm": 5.8,
    "sepalwidthcm": 2.7,
    "unnamed:_3": 5.1,
    "petalwidthcm": 1.9,
    "species": "Iris-flower"
  }
];

const distinctCount = (data, group, key) => {
    return Array.from(new Set(data.map(d => d[group])))
        .map(g => {return {
        [group]: g,
        [key]: data.filter(d => d[group] == g).length
  }});
}

console.log(distinctCount(data, "species", "sepallengthcm"));

Comments

1

I used underscore.js countBy method for that.

data = [
        {
            "index": 0,
            "id": 47,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 3.8,
            "unnamed:_3": 1.6,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 1,
            "id": 48,
            "sepallengthcm": 4.6,
            "sepalwidthcm": 3.2,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 2,
            "id": 49,
            "sepallengthcm": 5.3,
            "sepalwidthcm": 3.7,
            "unnamed:_3": 1.5,
            "petalwidthcm": 0.2,
            "species": "jennifer"
        },
        {
            "index": 3,
            "id": 50,
            "sepallengthcm": 5.0,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 1.4,
            "petalwidthcm": 0.2,
            "species": "setosa"
        },
        {
            "index": 4,
            "id": 97,
            "sepallengthcm": 12.0,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.2,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 5,
            "id": 98,
            "sepallengthcm": 6.2,
            "sepalwidthcm": 2.9,
            "unnamed:_3": 4.3,
            "petalwidthcm": 1.3,
            "species": "jennifer"
        },
        {
            "index": 6,
            "id": 99,
            "sepallengthcm": 5.1,
            "sepalwidthcm": 2.5,
            "unnamed:_3": 3.0,
            "petalwidthcm": 1.1,
            "species": "kajol"
        },
        {
            "index": 7,
            "id": 100,
            "sepallengthcm": 11.0,
            "sepalwidthcm": 2.8,
            "unnamed:_3": 7.0,
            "petalwidthcm": 1.3,
            "species": "floaw"
        },
        {
            "index": 8,
            "id": 101,
            "sepallengthcm": 6.3,
            "sepalwidthcm": 3.3,
            "unnamed:_3": 6.0,
            "petalwidthcm": 2.5,
            "species": "Iris-flower"
        },
        {
            "index": 9,
            "id": 102,
            "sepallengthcm": 5.8,
            "sepalwidthcm": 2.7,
            "unnamed:_3": 5.1,
            "petalwidthcm": 1.9,
            "species": "Iris-flower"
        }
        ];

let a = _.countBy(data, function(item) {
  return item.species;
});

tmp = [];
for (const key in a) {
  val = {}
  val.species = key;
  val.sepallengthcm = a[key]
  
  tmp.push(val);
}

console.log(tmp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.12.0/underscore-min.js" integrity="sha512-BDXGXSvYeLxaldQeYJZVWXJmkisgMlECofWFXKpWwXnfcp/R708nrs/BtNLH5cb/5TE7aeYRTDBRXu6kRL4VeQ==" crossorigin="anonymous"></script>

Example https://jsfiddle.net/8Lph7503/

Source: https://underscorejs.org/#countBy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.