Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an array of distinct values for a column from dojo/store

Tags:

json

arrays

dojo

My Scenario:

This is my test data in JSON Array format:

var testDataObj = [
  { col1: 'p0',   col2: 1, tbl: 'p',   col3: '14/01/2013', col4:'BOB'},
  { col1: 'p1',   col2: 2, tbl: 'p',   col3: '14/01/2013', col4:'SANDY'},
  { col1: 'p2',   col2: 3, tbl: 'p',   col3: '14/01/2013', col4:'JASON'},
  { col1: 'p3',   col2: 4, tbl: 'p',   col3: '14/01/2013', col4:'JASON'},
  { col1: 'p4',   col2: 5, tbl: 'p',   col3: '14/01/2013', col4:'SANDY'},
];

I created an object of dojo/store/Memory as following:

require(["dojo/store/Memory"], function(Memory){
  var gctsTestStore = new Memory({data: testDataObj});
});

What I want to achieve:

I want to get an array of distinct values of column col4:

var distinctColumnValues=gctsTestStore.getDistinctValuesFromColumnName('col4');
//distinctColumnValues= ['BOB','SANDY','JASON']

My Request:

Currently, I am looping through JSON Array testDataObj to achieve the result, but I am exploring a more elegant solution using dojo/store's API.

I am using dojo/store/Memory here but I can also accept solution for other type of dojo/store.

Thanks.

like image 724
user2461468 Avatar asked Jan 20 '26 07:01

user2461468


1 Answers

There are no built in APIs for getting Distinct values from Memory Store. You need to write you own method in Javascript for getting the distinct. Below is one way of writing it.

var unique = {};
var distinct = [];
for( var i in array ){
 if( typeof(unique[array[i].col4]) == "undefined"){
  distinct.push(array[i].col4);
 }
 unique[array[i].col4] = 0;
}
like image 74
frank Avatar answered Jan 23 '26 03:01

frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!