简体   繁体   中英

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

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM